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 24x7 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.
-
see Green fields, blue skies, and the white board - what is too broad?gnat– gnat2017年07月18日 12:42:06 +00:00Commented Jul 18, 2017 at 12:42
-
2you can have a windows process listen on a port. but... you shouldn't need to. try to redesign so that it can be running 24/7Ewan– Ewan2017年07月18日 13:23:07 +00:00Commented Jul 18, 2017 at 13:23
1 Answer 1
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.
-
Make sense :) I did not think about the paradox. Will go the web api route. Thanks.PushCode– PushCode2017年07月18日 15:02:38 +00:00Commented Jul 18, 2017 at 15:02