Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
jochance15-Feb-24 12:32
jochance15-Feb-24 12:32 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
Richard MacCutchan15-Feb-24 20:55
mveRichard MacCutchan15-Feb-24 20:55 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
jschell15-Feb-24 11:44
jschell15-Feb-24 11:44 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
jochance15-Feb-24 12:38
jochance15-Feb-24 12:38 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
Pete O'Hanlon15-Feb-24 19:10
mvePete O'Hanlon15-Feb-24 19:10 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
Richard MacCutchan15-Feb-24 20:54
mveRichard MacCutchan15-Feb-24 20:54 
GeneralRe: Nuget Dependency Problem (NServiceBus) Pin
jschell16-Feb-24 7:15
jschell16-Feb-24 7:15 
QuestionGracefully exit a .net 8 based windows service [SOLVED] Pin
Richard Andrew x6410-Feb-24 13:01
professionalRichard Andrew x6410-Feb-24 13:01 
It seems that the .NET 8 infrastructure code that interacts with the service control manager provides a layer of abstraction over the Start and Stop commands.

Therefore I don't see how to directly issue a service stop command. The service is passed a CancellationToken object, but I've been informed that you can't cancel such a token without access to the CancellationTokenSource.

When the service runs, all I have is the token, not the source.

So how do I shutdown and exit the service from code running inside the service?

SOLUTION:

Below is the template code produced by Visual Studio when you create a .NET 8 Windows service:

I simply created a property on the service class itself that can be set from inside the service and indicates to the outside world whether the service wants to shut down.

I called it StopFlag.



public sealed class WindowsBackgroundService(
    MyWindowsService myWindowsService,
    ILogger<WindowsBackgroundService> logger) : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        try
        {
            myWindowsService.OnStart(new string[0]);

            while (!stoppingToken.IsCancellationRequested && !myWindowsService.StopFlag)
            {
                await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
            }
        }
        catch (OperationCanceledException)
        {
            // When the stopping token is canceled, for example, a call made from services.msc,
            // we shouldn't exit with a non-zero exit code. In other words, this is expected...
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "{Message}", ex.Message);

            // Terminates this process and returns an exit code to the operating system.
            // This is required to avoid the 'BackgroundServiceExceptionBehavior', which
            // performs one of two scenarios:
            // 1. When set to "Ignore": will do nothing at all, errors cause zombie services.
            // 2. When set to "StopHost": will cleanly stop the host, and log errors.
            //
            // In order for the Windows Service Management system to leverage configured
            // recovery options, we need to terminate the process with a non-zero exit code.
            Environment.Exit(1);
        }
    }
}

The difficult we do right away...
...the impossible takes slightly longer.


modified 11-Feb-24 15:36pm.

AnswerRe: Gracefully exit a .net 8 based windows service Pin
OriginalGriff10-Feb-24 19:53
mveOriginalGriff10-Feb-24 19:53 
GeneralRe: Gracefully exit a .net 8 based windows service Pin
Richard Andrew x6411-Feb-24 4:47
professionalRichard Andrew x6411-Feb-24 4:47 
AnswerRe: Gracefully exit a .net 8 based windows service Pin
Gerry Schmitz11-Feb-24 9:03
mveGerry Schmitz11-Feb-24 9:03 
AnswerRe: Gracefully exit a .net 8 based windows service [SOLVED] Pin
jschell13-Feb-24 4:50
jschell13-Feb-24 4:50 
QuestionHaving trouble opening a registry key Pin
Richard Andrew x648-Feb-24 14:57
professionalRichard Andrew x648-Feb-24 14:57 
AnswerRe: Having trouble opening a registry key Pin
Dave Kreskowiak8-Feb-24 16:27
mveDave Kreskowiak8-Feb-24 16:27 
AnswerRe: Having trouble opening a registry key Pin
OriginalGriff8-Feb-24 20:02
mveOriginalGriff8-Feb-24 20:02 
GeneralRe: Having trouble opening a registry key Pin
jochance13-Feb-24 9:30
jochance13-Feb-24 9:30 
GeneralRe: Having trouble opening a registry key Pin
OriginalGriff13-Feb-24 19:56
mveOriginalGriff13-Feb-24 19:56 
AnswerRe: Having trouble opening a registry key Pin
Richard Deeming8-Feb-24 22:47
mveRichard Deeming8-Feb-24 22:47 
AnswerRe: Having trouble opening a registry key Pin
Eddy Vluggen12-Feb-24 12:36
professionalEddy Vluggen12-Feb-24 12:36 
RantGlobal using? Pin
jschell8-Feb-24 4:42
jschell8-Feb-24 4:42 
GeneralRe: Global using? Pin
Richard Deeming8-Feb-24 5:19
mveRichard Deeming8-Feb-24 5:19 
GeneralRe: Global using? Pin
jochance13-Feb-24 9:34
jochance13-Feb-24 9:34 
QuestionHow to set up a Using..? Pin
glennPattonWork38-Feb-24 1:32
professionalglennPattonWork38-Feb-24 1:32 
AnswerRe: How to set up a Using..? Pin
Richard Andrew x648-Feb-24 1:38
professionalRichard Andrew x648-Feb-24 1:38 
GeneralRe: How to set up a Using..? Pin
glennPattonWork38-Feb-24 2:24
professionalglennPattonWork38-Feb-24 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.