Friday, December 26, 2014

ASP.Net Web api self hosting using windows service

In my previous article, we discussed about web api self hosting using console application. This time, we will use the windows service to perform the self hosting of the web api. So let's start by creating a new windows service project type. We call it as WindowsService_HostAPI. 
In order to perform the self hosting of web api, we need to add references to the self hosting libraries. For this, we use the nuget package manager and install the references.

Next, we select the project in which we need to add the references and the following libraries get added to the project.
Next, we add a new item of the type web api controller class and name it as ValuesController, which inherits from ApiController. Make sure that the suffix of the class is  'Controller'.  This is required for the routing process to handle the incoming request. We remove all the default methods and add a simple method named GetString which returns a string value.

Now we need to configure a hosting server which would host the api. The point here is that this server must get configured in an event, which gets fired when the service is started. So we will use the OnStart event of our service class (which inherits from ServiceBase class). When the service is hosted, this OnStart event will be fired and the hosting configuration will be registered. So our code becomes:

Build the solution. Now the api is ready to be hosted and for this, we only need to install the service and than start it by navigating to the list of installed services.
To host this windows service, go to the service design view, right click and select 
Add Installer

1. Go to the properties of serviceInstaller1 and change the ServiceName property to WebAPISelfHosting. This will be name with which the service will get installed.
2. Go to the properties of serviceProcessInstaller1 and change Account property to LocalSystem.
To install the service, we need to use the installutil.exe from the location:
or you can start the visual studio command prompt (as an administrator) and use the following command to refer to the above location of installutil.exe.
cd  C:\Windows\Microsoft.NET\Framework\v4.0.30319
Execute the following command in the command prompt to install the service.
installutil ...\WindowsService_HostAPI\WindowsService_HostAPI\bin\Debug\WindowsService_HostAPI.exe
I have used only relative path here for the example. Make sure that you use the complete path, from the root. This will install the service on the system.

Go to the list of installed windows services, select the service named WebAPISelfHosting, right click and start the service.

Web api is now hosted and we can create a client to generate request to the api. For this we create an html page which will make an ajax call to the api.

Run the client page and also start the network capture using F12. See the results, web api returns the results.

So this is how we can host the web api using a windows service. Hope you enjoyed reading it. Happy coding...!!!

No comments:

Post a Comment