0

I found a tutorial that uses Topshelf to create a service and run it within Visual Studio. It worked, because the service did not need any input from outside; it just kept doing something on a timer. But if I create a service, using Topshelf, that accepts commands from another process, and run it within Visual Studio, is there anyway to connect to the service from another process? Or is it impossible?

For example, if the service has this,

    public void OnCustomCommand(int command)
    {
        Console.WriteLine("Got " + command);

and I ran the service in Visual Studio. Can I connect to the service from another process using a code like this?

ServiceController sc = new ServiceController("YOURServiceName", Environment.MachineName);
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "YOURServiceName");//this will grant permission to access the Service
scp.Assert();
sc.Refresh();

sc.ExecuteCommand((int)YourMethods.methodX);
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • https://stackoverflow.com/questions/7791471/communication-between-windows-service-and-desktop-app – Hans Passant Jan 04 '20 at 23:06
  • Afaik, topself creates a simple console application to start a service. You can attach to this process for debug – Pavel Anikhouski Jan 05 '20 at 08:23
  • I suspected that in the debug mode, it is not really a service so another process cannot connect to it using regular way (`ServiceController`), and I was wondring if Topshelf provides any framework to simulate sending commands, sort of like a fake `ServiceController` that I can use in another application. I guess there is no such thing. – Damn Vegetables Jan 05 '20 at 14:12

0 Answers0