1

I want to make a windows service worker, as described in this walkthrough: https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service.

I also want the user to see a tray icon to manipulate the service. To do this, I will create a second executable which runs in the user context and communicates with the service.

How should the app communicate with the service to perform things like checking the service status, restarting the service, etc.?

Apparently this could be done with .NET Framework by using a ServiceController in a user-run app to talk to the service (which I guess used to be a ServiceBase instead of a BackgroundService).

What is the equivalent "ServiceController" class, when using the newer BackgroundService/service worker model of .NET Core? Does ServiceController still work?

MHebes
  • 2,290
  • 1
  • 16
  • 29
  • I'm saying the same thing Mathis says below just using different words. There is no difference between a service and a regular application except the credentials and the environment. Normally a service is start when the machine is started and runs with a System account and no environmental variables. So when a service fails it is due to one of these two items being different. – jdweng Feb 15 '22 at 20:15
  • My question was unclear as to what exactly I was asking about, I've edited it. I also now believe that ServiceController will still work with the newer way to write services, since they're still windows services, just written differently. If that turns out to be true I will answer my own question later. – MHebes Feb 15 '22 at 20:19

1 Answers1

1

A typical pattern is to have two apps. You can't really do this. as services run in a different window station than the logged in user, so you can't have a system tray icon for those users. This is from the Microsoft docs https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications:

Windows Service applications run in a different window station than the interactive station of the logged-on user. A window station is a secure object that contains a Clipboard, a set of global atoms, and a group of desktop objects. Because the station of the Windows service is not an interactive station, dialog boxes raised from within a Windows service application will not be seen and may cause your program to stop responding. Similarly, error messages should be logged in the Windows event log rather than raised in the user interface.

The Windows service classes supported by the .NET Framework do not support interaction with interactive stations, that is, the logged-on user. The .NET Framework also does not include classes that represent stations and desktops. If your Windows service must interact with other stations, you will need to access the unmanaged Windows API. For more information, see the Windows SDK documentation.

The interaction of the Windows service with the user or other stations must be carefully designed to include scenarios such as there being no logged on user, or the user having an unexpected set of desktop objects. In some cases, it may be more appropriate to write a Windows application that runs under the control of the user.

Here are a couple of links about how to write to the system tray I found on internet and other post. You'll need another application to interface with the service, since the service can't directly have an icon in the system tray for windows..

How can I make a .NET Windows Forms application that only runs in the System Tray?

and

http://msdotnetsupport.blogspot.com/2008/02/cnet-application-windows-system-tray.html

Way to communicate with the windows service could be as follows:

mathis1337
  • 1,426
  • 8
  • 13
  • 1
    I too have seen [that answer](https://stackoverflow.com/a/2652285/3554391) :) My question is how to create a service controller, not a service. The "You'll need another application" part—I'm asking about what the other application would require in .NET Core. – MHebes Feb 15 '22 at 20:10
  • You could just create a windows form that appears in the tray icon that communicates with the windows service. – mathis1337 Feb 15 '22 at 20:11
  • Yes, I know, my question is how the communication happens. In .NET Framework, the answer appears to be "Use the ServiceController class". Is the answer the same in .NET Core with BackgroundService? – MHebes Feb 15 '22 at 20:12
  • I will update my answer. OK Updated – mathis1337 Feb 15 '22 at 20:13