Search code examples
azure-service-fabric

Service Fabric Guest Executable hosting and graceful stop


I have a .NET 4.6.2 console application I want to host in Service Fabric. My application is listening on console input events for administration tasks.

The most important one is 'q' to stop the application - but in a graceful manner.

Is it somehow possible to also have this functionality in Service Fabric with a guest executable?
Of course not on 'q button pressed' but rather on service deleted or whatever events there are.

If there is no way to achieve this kind of integration with a guest executable - which project template do you recommend to build upon?


Solution

  • In your stateful or stateless service class:

    class MyService : StatefulService /* or StatelessService */
    {
        protected override Task OnCloseAsync(CancellationToken cancellationToken)
        {
            // your graceful shutdown code goes here
        }
    }
    

    But execution of that method is not guaranteed, for instance when the hosting VM unexpectedly dies.