Building a rest service with ASP. WEBAPI (vi)--self-host

Source: Internet
Author: User

The ASP. Net. Webapi generates an assembly, not a standalone process, so it must be hosted on the appropriate host when it is run, and IIS hosting is generally more common. Many times, in order to simplify deployment or functional integration, we need to carry it to a separate process, which is generally referred to as self-host, this article briefly introduces the Webapi Sefhost method.

First install the Microsoft.AspNet.WebApi.SelfHost library on NuGet.

Attached to our WEBAPI controller

PublicClassValuescontroller:Apicontroller
    {
        < Span style= "color:blue;" >publicienumerable <string> Get ()
        {
             returnnew< Span style= "color:black;" >string[" { };
        }
    }

The next task is to start our server.

ClassProgram
{
StaticvoidMain (String[] args)
{
VarConfig =NewHttpselfhostconfiguration("Http://localhost:8080");

Config. Routes.maphttproute (
"API Default","Api/{controller}/{id}",
New{id =Routeparameter. Optional});

Using(VarServer =New              {
                 server. OpenAsync (). Wait ();
                console                 console             }
        }
    }

As you can see from the code above, the configuration file is basically the same as the ASP, and we can use the Webapiconfig directly if the DLL generated in the ASP . Register method to be configured. The following paragraph is to start the server, there is not too much good introduction ah. For more information, see the MSDN documentation: HTTP://WWW.ASP.NET/WEB-API/OVERVIEW/HOSTING-ASPNET-WEB-API/SELF-HOST-A-WEB-API

It is worth mentioning that Selfhost is in a separate process to start the HTTP service, or it can be said, it is a mini version of the HTTP server, I previously introduced through the HttpListener implementation of a simple HTTP service, to the present, With the Webapi Selfhost way is faster to achieve a better extension of the Mini HTTP server, when we need a simple HTTP service, you can use this way.

In addition to this approach, Microsoft is more recommended to use more powerful Owin to carry the WEBAPI service, because the current owin has not been how to study, the follow-up time to introduce it.

In addition, in addition to IIS, Selfhost and other ways, the cloud era is very convenient to publish to Windows Azure, there is not much to do here, see the MSDN documentation for more information: http://www.asp.net/web-api/overview/ Hosting-aspnet-web-api.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.