Skip to content

Host shuts down immediately  #3195

@rachelappel

Description

@rachelappel

Moved from the docs repo to here. Originally filed by @srininz77 at dotnet/AspNetCore.Docs#6862

"I'm trying to run a long running background task in a console application inside docker container.

My code is as follows:

 public static async Task Main(string[] args)
 {
            var hostBuilder = new HostBuilder()
                .ConfigureAppConfiguration
                (
                    (hostBuilderContext, configurationBuilder) =>
                    {
                        configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
                        configurationBuilder.AddEnvironmentVariables();
                        configurationBuilder.AddJsonFile("appsettings.json", optional: true);
                    }
                )
                .ConfigureServices
                (
                    (hostBuilderContext, services) =>
                    {
                        services.AddHostedService<HostedService>();
                        services.AddSingleton<ISocketImplementation, SocketImplementation>();
                        services.AddSingleton<IMessageHandler, MessageHandler>();
                    }
                );               

            await hostBuilder.RunConsoleAsync();
   }

**When I use Docker run -d to create the container, the container stops immediately. It doesn't look like RunConsoleAsync() is waiting for ctrl+c or SIGTERM.

I tried GenericHostSample and it behaves the same as well**"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions