C# – Creating a Windows Service with HTTP Commands in .NET

cnet

I have to write an windows service in .NET 4.6/C#, which reads messages from RabbitMQ queues and does some processing based on message type. Processing includes reading the message and based on business rules, insert/update SQL database and send out another message confirming the successful processing of the message received.

Entire service is done implementing OnSatrt() and OnStop() events in ServiceBase class. Reading queues and processing is all completed. The missing piece for me here is how to have the same windows service to accept http commands for starting, stopping and restarting the service. How can I design a windows service with http capabilities and 24×7 processing logic within it?

I am not looking for a whole design or entire code here, just need an idea on how a windows service in .NET can accept http commands while its processing data from queues.

Best Answer

Although it is certainly possible for a Windows service to listen for http requests (that's what IIS does after all), you cannot have a Windows service listen for commands to start and stop itself. That would be a paradox. How can your service listen for a start command before it is started?

The key is to have a second service control the first one. Write a small web service to accept the start/stop/restart commands, host it in IIS, and pass the commands down to the Windows service control manager. You can use any convenient technology for this, ASP.NET Web API or WCF will do fine.