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

Error occurs on .net Core 2.0 based Services #38

Closed barissonmez closed 7 years ago

barissonmez commented 7 years ago

Hi, Library fails while running on .net Core 2.0 based service applications.

Error that occurs while application running: Unhandled Exception: System.ComponentModel.Win32Exception: The service process could not connect to the service controller at DasMulli.Win32.ServiceUtils.Win32ServiceHost.RunAsync() at DasMulli.Win32.ServiceUtils.Win32ServiceHost.Run() at QRAuth.UsageService.Program.Main(String[] args) in C:\Users...\Program.cs:line ...

Event Viewer logs: applicationerror_details applicationerror_general information_details information_general

dasMulli commented 7 years ago

hi!

What version of windows are you running on?

Could you try adding https://ci.appveyor.com/nuget/dotnet-win32-service as a NuGet Source and use the version 1.1.0-master-000080 to see if it helps?

You wrote "while application runs" - does it fail immediately or run for some time and then crash? (assuming it was started as a windows service)

barissonmez commented 7 years ago

My OS is Windows 10 Pro.

I tried with version 1.1.0-master-000080 and 1.1.0 but getting same error and dotnet.exe crashs.

I' m trying "dotnet run" and "dotnet run --register-service" commands separately but getting same result. In CMD screen cursor waits for a while but suddenly returns error after waiting between 20-30 seconds. Additionaly myservice could not be seen in Services window.

dasMulli commented 7 years ago

just to make sure: Are you running the cmd as Administrator?

barissonmez commented 7 years ago

Yes, running it as Administrator.

dasMulli commented 7 years ago

Good/bad news: compiling the sample (sample/Testservice) on 2.0.0 using the released 1.0.1 version by replacing the project reference with a package reference (needs reverting to CreateService from CreateOrUpdateService) works fine on my win10 1703 enterprise. Could you try the same thing on your machine?

Did this work with .net core 1.0 or 1.1 before on the same machine?

pkjaer commented 7 years ago

I'm getting the same error - also on Windows 10. It does not work on dotnet 1.1 either.

dasMulli commented 7 years ago

@pkjaer does the sample service work? could you clone the latest master and try dotnet run --register-service from an admin command prompt in the sample app's directory and see if it registers a service and could start it? (should be visible in the service management)

The problem is that the code path in the stack trace looks fine, the win32 exception is a standard windows exception that can occur in the following scenario (according to some google research):

Only other reason I can think of at the moment is a mismatch between registered service names and names offered via the API call (=> the name property that the service class has to implement).

pkjaer commented 7 years ago

The sample service does work, yeah (provided that I specify "--framework netcoreapp2.0" in the run command).

I'll look into some of those suggestions, as well as the differences between the sample and my new service.

dasMulli commented 7 years ago

Thanks! Would be interested to hear the outcome.

pkjaer commented 7 years ago

Oh, actually -- the sample project also doesn't run if I do --run-as-service instead of --register-service.

dasMulli commented 7 years ago

yes because --run-as-service is supposed to be passed in by the service controller and not invoked manually.

dasMulli commented 7 years ago

you should get the same exception when you use ServiceBase.Run(…) on .net framework without any checks to Environment.UserInteractive (which works differently on core)

pkjaer commented 7 years ago

I understand. Is there any way to debug the service, though?

pkjaer commented 7 years ago

Alright, I was able to debug by adding a Thread.Sleep call into the Startup class and found the exception that was thrown. It was related to the current directory being System32 and the code not expecting that. In other words, unrelated to your code which seems to be working just fine. It's just a bit hard to troubleshoot issues, but I suppose there isn't much you can do about that :)

dasMulli commented 7 years ago

Yes that was always 💩 to debug even with ServiceBase. See related https://github.com/dasMulli/dotnet-win32-service/issues/18#issuecomment-267798630

dasMulli commented 7 years ago

Closing this because the discussion about running in and out of service environment has been resolved and haven't heard back from the OP. (Reopen / post with additional issues if it hasn't been resolved)