ASP.Net Web API Self Hosting

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 the hosting process. There are two ways to host a Web API:

  1. Self hosting
  2. IIS hosting
As the name suggests, self hosting, the Web API is hosted independently of the main/client application. So the actual Web API is hosted either in a Windows Service or a 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 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 the solution. Search the library and click Install.

NuGet Packages

Select the solution file to which we need to add the library.

SelectSolution

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, that is System.Web.Http.SelfHost.

ddlRequired


Next, we will be add a new item to the project, of type, Web API Controller class. We will call it ProductsController.cs. Here, we have the following two important points to take care of:

  1. The Web API controller must inherit from the APIController class.

  2. Suffix the name of the class with the "Controller" keyword, since the routing process will identify the API controllers as the ones that have the suffix "Controller" attached to them. This will help the routing procedure to process the incoming request corresondingly.
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:

Productdetailed

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 ensure 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 the Program.cs file.

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.

Routingtemplate

Next, we create an instance of the HttpSelfHostServer class and assign to it the configuration, defined above and start the server to listen to a HTTP request.

CreateInstance

If we hover over the name of this class, we can see its description as:

Implementation of an System.Web.Http.HttpServer that listens directly to HTTP

Which is very much self-explanatory, in other words 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 AJX GET request to the method we created in the preceding. There is no difference in how we make the AJAX request to the Web API. It remains the same as we would have done in our normal application.

NormalApplication

Run the application. Also start the network capturing in the browser to see the results.

See the response body contains the results returned by the API, as 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 encounter the following issue, when you start the Web API application:

{"HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see: Microsoft }

ResultScreen

This is something related to the permissions. Without going much into the depth of the issue, just restart the Visual Studio as an administrator and start the application and it will be fine. So this is how we do the self-hosting of a Web API.


Similar Articles