0

I have .net core console application, which is hosted as windows service.
I want to catch an event if the user logs off/shutdown the computer.

I have found ways to catch this event in .net framework (here & here).
But I cant figure out how to achieve this in .net core.

To create service I am using "ServiceBase" class. Sample code is as given below:

public class MyService : ServiceBase
{
    readonly string LogPath = "D:\\TestAppService.txt";

    #region Constructors
    public MyService()
    {
        this.CanShutdown = true;
    }
    #endregion

    #region Protected Functions
    protected override void OnStart(string[] args)
    {
        //your code here

        // call the base class so it has a chance
        // to perform any work it needs to
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        //your code here

        // Call the base class 
        base.OnStop();
    }

    protected override void OnShutdown()
    {
        using (StreamWriter sw = File.AppendText(LogPath))
        {
            sw.WriteLine("shutdown == true");
        }

        //your code here
        base.OnShutdown();
    }
    #endregion
}  

The OnStop and OnStart methods are being called.
but when I shutdown the computer my OnShutdown method is not called.

RPrashant
  • 217
  • 3
  • 13

2 Answers2

0

According to aspisof.net, you should be able to use the SessionEnding API. This is because it is listed as being exposed in the windows Compatibility Pack - available on NuGet here.

This article on learn.microsoft.com shows how you can include it in a .NET Core application.


tl;dr

  1. Add the NuGet package
  2. Target Windows only

One thing to note: this was originally designed to be a temporary fix for porting Windows specific .NET code over to .NET Core.

The more accepted way to implement Windows only features is to move as much code to .NET Standard libraries as possible, and to use conditional compilation directives to include platform specific code when building for that platform.

Jamie Taylor
  • 1,644
  • 1
  • 18
  • 42
  • thanks for the help. But Console applications(Hosted as windows service) do not raise the SessionEnding event. In a Windows service, unless a hidden form is used or the message pump has been started manually, this event will not be raised. The actual problem is in.NET Core 2.2 Assembly System.Windows.Forms missing as mentioned [here](https://stackoverflow.com/questions/52986985/net-core-2-2-assembly-system-windows-forms-missing) – RPrashant Jun 25 '19 at 11:24
  • I have updated my question could you please help me? – RPrashant Jun 27 '19 at 05:22
0

By design dotnet core is not "friendly" with platform specific stuff (like listening to log off event seems to me).

The solution I use in one of Windows-hosted services is described here.

When application domain is forced to close by operating system on shutdown - there is a room for using AppDomain event handlers.

Artyom Ignatovich
  • 591
  • 1
  • 3
  • 13