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

Getting error "The service process could not connect to the service controller" #71

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi Martin,

Am getting the following error while running directly in my console application. have attached an image.

Great library btw. Thanks error

dasMulli commented 6 years ago

When you run an app directly you can’t use the windows service functionality. It is only available when started from windows‘ service system. The demo application code in this repo checks for command line arguments that are added in the registration.

How does your startup code (Main function) look like?

ghost commented 6 years ago

`namespace MyConsoleWindowsService { public class Program { private static IConfiguration Configuration { get; set; }

    static void Main(string[] args)
    {
        var configBuilder = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        Configuration = configBuilder.Build();

        var container = IocConfig();
        ICommand cmd = container.Resolve<Method1Command>();

        var myService = new RunAsService(cmd);
        var serviceHost = new Win32ServiceHost(myService);
        serviceHost.Run();

        Console.WriteLine("---------- PRESS ANY KEY TO EXIT ----------");
        Console.ReadKey();
    }

} }`

dasMulli commented 6 years ago

Yeah serviceHost.Run() will only work when started from the windows service subsystem as described above.

dasMulli commented 6 years ago

Closing since behaviour is as designed.