Reputation: 4990
If I create a Windows Service, is there a way to call that service from asp.net?
Upvotes: 8
Views: 20070
Reputation: 36700
As an addition, for sending a (really) simple message, also
ServiceController.ExecuteCommand(int command)
could be used. With the usage of executecommand
it's really easy to send an integer
/enum
as message.
var myService = new ServiceController("SimpleService");
myService.ExecuteCommand(128);
myService.ExecuteCommand((int)SimpleServiceCustomCommands.ScanFiles);
and myService
looks like:
public class myService : ServiceBase
{
...
protected override void OnCustomCommand(int command)
{
//executes logic
Upvotes: 1
Reputation: 5082
Some clarity would help - you could also interpret "call that service" as some sort of remote method call from ASP.NET to your service - e.g. to store some state in your Windows Service.
Host a service endpoint in the Windows Service e.g. using Remoting, WCF, or plain ol' TCP socket server. All of which could be called by clients hosted in ASP.NET.
There are plenty of remoting and WCF examples out there that do just that - search for hosting and one of those technologies.
Some MSDN linkage to read:
You should also read about Windows Process Activation - you may be able to get away without writing a seperate service.
Upvotes: 3
Reputation: 7830
By
"Call that service"
do you mean, manage the service? As in, Start, Stop, Restart etc? If so, then the answer is yes. There is a good article at www.csharp-examples.net that will show you how to use the ServiceController class.
If however, you mean send messages to it, as if it's a Web Service, then, the answer is, that depends on how you're exposing your service. Windows Services can host WCF services, so that would be a good option, if you wanted to expose your windows service to internet clients.
Upvotes: 9