dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

In official Docker mcr.microsoft.com/dotnet/aspnet Windows image using Microsoft.Extensions.Hosting.WindowsServices, service doesn't respond to control requests #29898

Closed The-MAZZTer closed 3 years ago

The-MAZZTer commented 3 years ago

Describe the bug

When using the Microsoft.Extensions.Hosting.WindowsServices package, you can write services that can be installed and run. Hooray!

However if you add the same service to the official Docker container mcr.microsoft.com/dotnet/aspnet the service starts but fails to respond to the start control request and times out.

Adding logging shows .UseWindowsService is being called.

The same app works fine outside of the docker container in a full Windows environment. The same app also works inside the docker container with .UseWindowsService not called when run as a normal user process.

To Reproduce

I used the basic Empty ASP.NET Core 3.1 project template from the latest VS2019 and added some code in Program.cs to allow for easy service management (uninstall/install using sc.exe and SystemServiceProcess.ServiceController package). Otherwise it's unmodified.

I used VSCode to write the Docker support instead of VS2019 (since that's how I did it for the project where I first identified this problem).

I've attached the resulting reproduction.

TestService.zip

This uses .NET 5.0.

With the completed project:

  1. Build and publish using FolderProfile.
  2. Build Dockerfile (I used VSCode's Docker extension to do this).
  3. Error shown below happens on build as Dockerfile attempts to install service inside container.
  4. Tun the project with Kestrel/IIS Express from VS directly to show when not running as a service it runs fine.
  5. Run the build binary as admin with an --install argument to install and start the service on the host machine to verify it runs fine (Use --uninstall to remove).
  6. Start a cmd.exe in the intermediate docker container made during the failed docker build and run TestService directly to verify it runs fine inside the container when not a service (IIRC the command is: docker run -a -i cmd)

Exceptions (if any)

Unhandled exception. System.InvalidOperationException: Cannot start service 'TestService' on computer '.'. ---> System.ComponentModel.Win32Exception (1053): The service did not respond to the start or control request in a timely fashion. --- End of inner exception stack trace --- at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at TestService.Program.Main(String[] args) in C:\Users\\Source\Repos\TestService\TestService\Program.cs:line 33

Further technical details

Service installs running u nder NetworkService user in my example, LocalSystem has the same issue. I couldn't get the service to install running under ContainerUser or ContainerAdministrator to see if that made a difference.

.NET SDK (reflecting any global.json): Version: 5.0.102 Commit: 71365b4d42

Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.102\

Host (useful for support): Version: 5.0.2 Commit: cb5f173b96

.NET SDKs installed: 2.1.201 [C:\Program Files\dotnet\sdk] 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.520 [C:\Program Files\dotnet\sdk] 2.1.617 [C:\Program Files\dotnet\sdk] 2.1.812 [C:\Program Files\dotnet\sdk] 2.2.107 [C:\Program Files\dotnet\sdk] 3.1.405 [C:\Program Files\dotnet\sdk] 5.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download

VS2019 Version 16.8.4 VSCode 1.45.1 (latest version approved for use in my org) Docker for Desktop 2.2.0.4 (43472) (latest version approved for use in my org; 3.1 installer is broken for me anyway) Windows 10 x64 1909 Build 18363.1316 (latest version blah blah blah you know the deal)

BrennanConroy commented 3 years ago

Looks like a dupe of https://github.com/dotnet/extensions/issues/2778

The-MAZZTer commented 3 years ago

Thanks, that looks like the issue. I will try the workaround code on Monday and see if that works for me.

On Fri, Feb 5, 2021, 12:17 PM Brennan notifications@github.com wrote:

Looks like a dupe of dotnet/extensions#2778 https://github.com/dotnet/extensions/issues/2778

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dotnet/aspnetcore/issues/29898#issuecomment-774168194, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC4AHP7Q7B6NRSBTNQ4XVLS5QR4NANCNFSM4XDBBJKA .

The-MAZZTer commented 3 years ago

The workaround listed did work for me. Thanks.