Blog
ASP.NET Web API: Self-Hosting
Excerpt by Phil Ledgerwood | March 21, 2013
Building a service-oriented architecture with the ASP.NET Web API does not restrict you to IIS deployment. This may come as a surprise to those who have worked with Microsoft technologies for a long time. Many Microsoft technologies for the web are designed to be deployed on IIS and become erratic or completely non-functional if deployed in some other way. The Web API, however, does not need to be deployed in IIS, or even deployed to a web server. Microsoft has put the tools in the framework necessary to host Web API services out of any application you write.
Hosting Web API services in your own process is called self-hosting. You might think that self-hosting is an intensely involved process that has to manage sockets, multiple threads, etc. However, the framework has two objects that wrap up most of this complexity for you: HttpSelfHostConfiguration and HttpSelfHostServer.
The HttpSelfHostConfiguration object provides information to the framework on how you want the host to work. At the very least, this includes specifying the root URL that the host will use for HTTP requests and the routing rule(s) used. If all you intend to host are Web API services, then you can simply copy and paste the routing rule from the Global.asax of a Web API application.
WARNING! Make sure the application that you're writing has adequate permissions to commandeer ports. Also, make sure that the machine running your application can receive HTTP requests from other machines on your network or outside your network depending on your needs. If your application can process requests made on that machine but not from any others, that could be a sign that the machine can't be accessed from the outside. Finally, make sure your application's build is targeted for the .NET 4 Framework and not the .NET 4 Framework Client Profile.
The HttpSelfHostServer takes in the second object: an HttpSelfHostConfiguration object. In your code, simply use the OpenAsync and Wait methods of this object for it to start listening for HTTP requests. Any HTTP requests that hit the URL defined in the HttpSelfHostConfiguration will run through the routing rules also defined in that configuration and get routed accordingly. From that point, everything works just like a normal Web API application. It's almost as if you've written your own miniature web server as part of some other program. This code is quite simple considering the complexity of what you're actually doing. Self-hosting can be useful in situations where IIS is not an option, where you have a very tightly-focused application that needs to handle everything internally, or you have various applications that need to communicate with each other across HTTP.
TIP: In order to use these objects, it is important that you have the MVC4 and selfhosting DLLs available to your code. The easiest way to get them is from the NuGet package Microsoft.AspNet.WebApi.SelfHost.
This post is an excerpt from the online courseware for our ASP.NET Web API Hosting and Dependency Resolution course written by expert Philip Ledgerwood.
Phil Ledgerwood
This course excerpt was originally posted March 21, 2013 from the online courseware ASP.NET Web API, Part 4 of 4: Hosting and Dependency Resolution by Phil Ledgerwood