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.
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
Next, we will add a class file which will be act as the
windows service to be hosted. We name it as
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:
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
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
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
Execute the following command in the command prompt to install the service.
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.
Go to the list of installed 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.
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…!!!