dasMulli / dotnet-win32-service

Helper classes to set up and run as windows services directly on .net core. A ServiceBase alternative.
MIT License
451 stars 57 forks source link

Subsequent failures options #76

Closed ka4ep closed 6 years ago

ka4ep commented 6 years ago

I'm using some 1-2 years old copied versions of this win32 service and Peter Kottas' wrapper for an ordinary .NET 4.7 project. Due to use of some 3rd party legacy API I have a demand to programmatically restart service occasionally to release that API's unmanaged resources. Old bugger leaks memory and handles. So, somewhere deep in code this block

Environment.ExitCode = -1; // Results in a fake failure
var p = Process.GetCurrentProcess();                        
p.Kill();

initializes a stops service. The only thing lacks for a proper restart are the following options: image Not the end of the world, but local admin may forget to set these up manually after update. As I haven't sync'd this project with my copy for a while, are these options already implemented? If not, are there plans for these properties' implementation?

dasMulli commented 6 years ago

That has been implemented in https://github.com/dasMulli/dotnet-win32-service/pull/36

var serviceDefinition = new ServiceDefinitionBuilder(ServiceName)
    .WithDisplayName(ServiceDisplayName)
    .WithDescription(ServiceDescription)
    .WithBinaryPath(fullServiceCommand)
    .WithCredentials(Win32ServiceCredentials.LocalSystem)
    .WithAutoStart(true)
    .WithFailureActions(new ServiceFailureActions(TimeSpan.FromDays(1), null, null, new[]{
        new ScAction
        {
            Delay = TimeSpan.FromMinutes(5),
            Type = ScActionType.ScActionRestart
        },
        new ScAction
        {
            Delay = TimeSpan.FromMinutes(5),
            Type = ScActionType.ScActionRestart
        },
        new ScAction
        {
            Delay = TimeSpan.FromMinutes(5),
            Type = ScActionType.ScActionRestart
        }
    }))
    .Build();

new Win32ServiceManager().CreateOrUpdateService(serviceDefinition, startImmediately: true);
ka4ep commented 6 years ago

I really need to upgrade and use as package. My copy's missing that bit. Thanks a lot for this example!