ASP.Net web api Self Hosting


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:

  1. Self hosting
  2. 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 Install. 

ASP.Net web API Self hosting

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

ASP.Net web API Self hosting

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

ASP.Net web API Self hosting

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 ApiController class.
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:

ASP.Net web API Self hosting

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 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.

ASP.Net web API Self hosting

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.

ASP.Net web API Self hosting

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.

ASP.Net web API Self hosting

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

ASP.Net web API Self hosting

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:

{"HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details)."}

ASP.Net web API Self hostingThis 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.

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in ASP.Net web API and tagged . Bookmark the permalink.

2 Responses to ASP.Net web api Self Hosting

  1. saidulu says:

    Hi Jasminder,
    I’m unable to upload and download file using asp.net web api please give any suggestion.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s