In this article, we will discuss the concept of self hosting the web api. We will not get into the details of what is a web api or why it should be used. Our main focus in this article will be on hosting process. There are two ways to host a web api:
- Self hosting
- IIS hosting
As the name suggests itself, self hosting, web api is hosted independent of the main/client application. So the actual web api is hosted either in
windows service or
console application running on the server. For our case, we will be using the
console application to host it. So let’s start with it.
Create a new
windows application. We will name it as
WebAPI_SelfHosting. We need to add the references to the
ASP.Net Web API Self host libraries to implement the web api. For this, we will use the nuget package. So go to
Tools -> Nuget Package Manager -> Manage NuGet Packages for Solution. Search the library and click
Select the solution file to which we need to add the library.
Click on ‘
I Accept‘ and the references will be added to the project. This will also install the dll’s required for the self hosting process which is
Next, we will be add a new item in the project, of type,
Web API Controller class. We will call it as
ProductsController.cs . Here, we have two important points to take care of:
1. Web api controller must inherit from the
2. Suffix the name of the class with ‘Controller’ keyword, as the routing process will identify the api controllers, as the ones, which have suffix ‘Controller’ attached to them. This will help the routing procedure to process the incoming request accordingly.
We also add a
Product class with some properties and a method named
GetProduct(). This will simply return a
Product detailed object. See the code below:
So our basic business logic is ready. Now we need to configure the api, so that it can be hosted. The basic requirement to configure the self hosting, is that we need to make sure that when the hosting application (
windows application or
windows service) starts, the required configuration is registered for the web api. So we will be writing our code in the
Main method in
We first define the self hosting configuration using the
HttpSelfHostConfiguration class. This will include declaring the host url and the routing template for the web api.
Next, we create an instance of the
HttpSelfHostServer class and assign to it the configuration, defined above and start the server to listen any HTTP request.
If we hover over the name of this class, we can see its description as:
Implementation of an System.Web.Http.HttpServer which listens directly to HTTP
which is very much self explanatory i.e. it creates a type of hosting server, like we have the IIS server for hosting our applications.
Now run the application and our web api is hosted and ready to be used by a client application. For this, we create an html page in an mvc application and make an ajax GET request to the method we created above. There is no difference in how we make the ajax request to web api. It remains the same as we would have done in our normal application.
Run the application. Also start the network capturing in browser to see the results.
See the response body contains the results returned by the api, as a
json. Although the api is being hosted in a
windows application, still it s being referred to as a self hosted web api. You may face the following issue, when you start the web api application:
This something related to the permissions. Without going much into depth of the issue, just restart the Visual Studio as an administrator and start the application and it will be fine. So this is we do the self hosting of a web api.