In my previous article, we discussed Web API self-hosting using a console application. This time, we will use a Windows Service to do the self-hosting of the Web API. So let's start by creating a new Windows Service project type. We call it WindowsService_HostAPI.
In order to perform the self-hosting of the 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 need to be added to the project.
Next, we add a new item of the type Web API controller class and name it ValuesController that inherits from APIController. Be sure that the suffix of the class is "Controller". This is required for the routing process to handle the incoming requests. We remove all the default methods and add a simple method named GetString that returns a string value.
Next, we will add a class file that will be act as the Windows Service to be hosted. We name it SelfHostService and will inherit from the ServiceBase class. Now we need to configure a hosting server that would host the API. The point here is that this server must get be configured in an event that is immediately executed when the service is started. So we will use the OnStart event of our service class (that inherits from the 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.
- Go to the properties of serviceInstaller1 and change the ServiceName property to WebAPISelfHosting. This will be name with which the service will be installed.
- Go to the properties of serviceProcessInstaller1 and change the 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 preceding location of installutil.exe.
Execute the following command in the command prompt to install the service.
I have used only a relative path for the example. Be 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.
The Web API is now hosted and we can create a client to generate a request to the API. For this we create an HTML page that will make an ajax call to the API.
Run the client page and also start the network capture using F12. See the results, the Web API returns the results.
So this is how to 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 otion. This is for the command to uninstall the Windows Service. Again, I have used the relative path to the exe of the Windows Service. You will be required to replace it with the path of your actual application location.
The sample code for the Windows Service is also attached here. Happy coding!