0

I´ve got a windows service and a NotifyIcon-Applicaiton (Sys-Tray) which should communicate with the service. The App should send a command and receive a PORT from the service for a socket which provides the further communication.

The Service overrides the OnCustomCommand Method but I don´t know how to send an answer to the calling-programm.

    protected override void OnCustomCommand(int command)
    {
        switch (command)
        {
            case 'p':
                // SEND Port number
                break;
            case 's':
                // SEND Service Name
                break;
            case 'n':
                // SEND Notification
                break;
        }

        base.OnCustomCommand(command);
    }

Notify-Icon-Application

var serviceController = new ServiceController("ATLED Service"); serviceController.ExecuteCommand((int)'p');

Felix Arnold
  • 839
  • 7
  • 35
  • AFAIK CustomCommand is "one-way". If your Service shall listen for client TCP(?) connections, it is common to just chose one that is well-known to clients. – Fildor Oct 28 '19 at 12:50
  • There is a socket which communicates with the service but the app doesn´t know the port which only the service knows. I consider a static port is not a good way (what will happen if the port is taken?) – Felix Arnold Oct 28 '19 at 16:05
  • What does IIS do if Port 80 is taken? If you really want to fortify against this, then you should write the chosen port to a config file and have the App check that file, too. Or alternatively, use the [registry](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-create-a-key-in-the-registry). – Fildor Oct 28 '19 at 16:18

1 Answers1

1

There can be different ways you can do that

  1. You can use a TCP port, which is listening for commands reply from server
  2. You can use signalR on the app side and connect service and app using that
  3. You can also use share memory or may be a file which is create/written by service when something is there for app, or can use a database as well
Atta H.
  • 661
  • 5
  • 11