ASP.Net Web API self hosting using windows service

In my previous article, we discussed about web api self hosting using a 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.

Web API  hosting using windows service

Next, we select the project in which we need to add the references and the following libraries get added to the project.

Web API  hosting using windows service

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.

Web API hosting using windows service

Next, we will add a class file which will be act as the windows service to be hosted. We name it as SelfHostService and will inherit from ServiceBase class. 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 immediately gets executed, 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:

Web API hosting using windows service

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.

Web API hosting using windows service

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 for the example. Make sure that you use the complete path, from the root. This will install the service on the system.

Web API hosting using windows service

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

Web API hosting using windows 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.

Web API hosting using windows service

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

Web API hosting using windows service

So this is how we can host the web api using a windows service. Also, you can uninstall the
windows service using the following command:

installutil /u ...\WebAPI_SelfHosting_WindowsService\WindowsService_HostAPI\WindowsService_HostAPI\bin\Debug\WindowsService_HostAPI.exe

Note the use of the ‘/u’ keyword. This is for the command to uninstall of the windows service. Again, I have used the relative path to the exe of the windows service. You would be required to replace it with the path of your actual application location.

Hope you enjoyed reading it. Happy coding…!!!

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.

I have a suggestion..

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

You are commenting using your 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