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.