Skip to content

UseWindowsService() does not work on docker containers #50020

Closed

Description

@drakeaan

Describe the bug

When creating a windows service of a .NET Core 3.0/3.1 project, the service does not start if it's running inside a container.

To Reproduce

Steps to reproduce the behavior:

  1. Create an ASP.NET Core 3.0 Project
  2. Implement the Hosting extension and add app.UseWindowsService()
  3. Create a docker container (.NET Framework) and install the .net core runtime
  4. Register the service and attempt to start it.

Expected behavior

The service should start.

Additional context

WindowsServiceHelpers checks if the SessionId == 0. In containers, the sessionid will never be 0.

return parent.SessionId == 0 && string.Equals("services", parent.ProcessName, StringComparison.OrdinalIgnoreCase);

Activity

  1. analogrelay commented on Dec 18, 2019

    @analogrelay
    Contributor

    Why are you running a Windows Service inside a Windows Container? That seems odd. Can you provide a runnable sample application (including Dockerfile) that reproduces the problem?

  2. drakeaan commented on Dec 19, 2019

    @drakeaan
    Author

    It's a legacy "lift-and-shift" and short-term we have to run the micro side-by-side with the legacy application. Long-term is to move them into separate containers.

    I will do.

  3. analogrelay commented on Jan 3, 2020

    @analogrelay
    Contributor

    @DewaldS Have you been able to set up a runnable sample? (I realize the holidays may have gotten in the way ;)). Let us know if you have such a sample.

  4. drakeaan commented on Jan 6, 2020

    @drakeaan
    Author

    @anurse Yeah... Sorry about that. With the holidays it was a bit difficult.

    Attached is a zip file with a very simple project and dockerfile. There is also a BuildContainer.ps1 to build and publish the project and create the docker image.

    When you create the container and exec into it, you'll notice the service remains in "Starting" for the default 30s period. After that it goes into a "Stopped" state as the service doesn't start.

    # For Testing
    docker run -it repro2778:latest powershell
    
    # In terminal inside container
    Get-Service WindowsCore31AsService

    Executing the exe manually, however, indicates that the exe does work.

    Below is the extension code we used to work around the problem (we removed the IsWindowsService() check).

    public static IHostBuilder UseWindowsServiceInContainer(this IHostBuilder hostBuilder)
    {
        // Host.CreateDefaultBuilder uses CurrentDirectory for VS scenarios, but CurrentDirectory for services is c:\Windows\System32.
        hostBuilder.UseContentRoot(AppContext.BaseDirectory);
        hostBuilder.ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddEventLog();
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
            services.Configure<EventLogSettings>(settings =>
            {
                if (string.IsNullOrEmpty(settings.SourceName))
                {
                    settings.SourceName = hostContext.HostingEnvironment.ApplicationName;
                }
            });
        });
    
        return hostBuilder;
    }

    Regards

    Repro2778.zip

  5. analogrelay commented on Jan 6, 2020

    @analogrelay
    Contributor

    Yeah... Sorry about that. With the holidays it was a bit difficult.

    No problem! Totally understandable. We'll take a look now that things are ramping back up here after the holidays 😉.

  6. self-assigned this
    on Jan 7, 2020
  7. removed their assignment
    on Jan 15, 2020
  8. mk185147 commented on Aug 26, 2020

    @mk185147

    We have the same issue. One thing that I don't see noted in the issue is that it isn't specific to containers. The same problem will appear on any Windows Server Core as it doesn't support SessionId 0 isolation.

  9. BrennanConroy commented on Oct 7, 2020

    @BrennanConroy
    Member

    Triage: We need to figure out a better heuristic for this. We should also consider an override switch "I know I can use WindowsService"

    Workaround for now is to copy the code the service adds

    hostBuilder.UseContentRoot(AppContext.BaseDirectory);
    hostBuilder.ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddEventLog();
    })
    .ConfigureServices((hostContext, services) =>
    {
        services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
        services.Configure<EventLogSettings>(settings =>
        {
            if (string.IsNullOrEmpty(settings.SourceName))
            {
                settings.SourceName = hostContext.HostingEnvironment.ApplicationName;
            }
        });
        services.Configure(configure);
    });
    
  10. matt-psaltis commented on Feb 10, 2021

    @matt-psaltis

    Hit this on .net 5 today. Lost another 5 hours thinking its me and the way I'm building the services. Thank you to those who helped identify this issue and the fix does still work on .net 5.

    Please fix!

  11. transferred this issue fromdotnet/extensionson Mar 22, 2021
  12. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Mar 22, 2021
  13. eerhardt commented on Aug 10, 2021

    @eerhardt
    Member

    Closing as a duplicate of #52416. That issue has the suggested solution, so using that one going forward.

  14. ghost locked as resolved and limited conversation to collaborators on Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions