Giter Site home page Giter Site logo

Comments (5)

MontyGvMC avatar MontyGvMC commented on July 30, 2024

I have found a question on StackOverflow: Killing gracefully a .NET Core daemon running on Linux

Whenever there is a need for being notified when the system shutsdown this line helps:

AssemblyLoadContext.Default.Unloading += MethodInvokedOnSigTerm

I used it in MyService : IMicroservice to call the Stop() method manually

from dotnetcore.windowsservice.

PeterKottas avatar PeterKottas commented on July 30, 2024

Hey mate, just came back from holidays and reading your issue. I must say I am not exactly sure how this should work. It feels like the stop should be called only when the service is stopped. Shutting down the pc is maybe not stopping per say. More like a termination. It might make sense to add another callback (OnShutdown) and use the solution that you proposed to call it. Or just call OnStop instead? Just thinking out loud here TBH.

from dotnetcore.windowsservice.

dasMulli avatar dasMulli commented on July 30, 2024

This would be possible in a custom state machine instead of IWin32Service.. I haven't had any request or the need for a shutdown event so I haven't implemented any convenience API for it. But the event can be registered for in windows' service management system. ServiceBase has an OnShutdown() function that can be used for this.

from dotnetcore.windowsservice.

z3r0privacy avatar z3r0privacy commented on July 30, 2024

I stumbled upon this issue a few days ago since I needed the hook on Shutdown too. So I gave it a try today and thanks to the good work of @dasMulli this was actually fairly easy to accomplish.

It's not a perfect solution but it works and is a good point to go.

All you need to do is:

  • Create an interface called IShutdownableWin32Service or something like that, which extends IWin32Service and add a method void Shutdown();
  • Change InnerService class to implement IShutdownableWin32Service and implement the missing method like the others are implemented already. You also want to add an argument for the shutdown action to the constructor
  • Add public Action<SERVICE> OnServiceShutdown { get; set; } in the HostConfiguration class.
  • In the ServiceRunner class you need to pass an argument for the new shutdown argument. Again, do this like the Stop(config) method using the new created action in the previous step.
  • Create the custom state machine. For this, copy the SimpleServiceStateMachine from here and name it ShutdownableServiceStateMachine. Apply a few changes:
    • Change the type of serviceImplementation to IShutdownableWin32Service. Don't forget to change this in the constructor too.
    • In the OnStart method, change the second line in the try block to statusReportCallback(ServiceState.Running, ServiceAcceptedControlCommandsFlags.Stop | ServiceAcceptedControlCommandsFlags.Shutdown, win32ExitCode: 0, waitHint: 0);
  • In the OnCommand method, add the following.
else if (command == ServiceControlCommand.Shutdown)
{
    try
    {
        statusReportCallback(ServiceState.StopPending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 3000); //this is probably too much, see note down below
        serviceImplementation.Shutdown();
    }
    catch { }
    finally
    {
        statusReportCallback(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 0);
    }
}
  • Back in the ServiceRunner class change the line where the Win32ServiceHost is initialized (in method ConfigureService) to var serviceHost = new Win32ServiceHost(config.Name, new ShutdownableServiceStateMachine(testService));
  • Add a OnShutdown method in the ServiceConfigurator class so you can use it from the configuration code.

Note: This should do the trick and also covers a system restart. Please note the remarks mentioned at MSDN about the SERVICE_CONTROL_SHUTDOWN control.

from dotnetcore.windowsservice.

PeterKottas avatar PeterKottas commented on July 30, 2024

Fixed by https://github.com/PeterKottas/DotNetCore.WindowsService/releases/tag/v2.0.7. Feel free to reopen if you find any issues with the current solution.

from dotnetcore.windowsservice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.