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

Startup Configure method will execute after BackgroundService ExecuteAsync method #24475

Closed cocosip closed 4 years ago

cocosip commented 4 years ago

Describe the bug

When i use BackgroundService in Asp.Net Core 3.1 project, i find that the Setup Configure method will execute after BackgroundService ExecuteAsync method. There are some initialize code in Configure method, so when BackgroundService have some injections, it will caught some exception.

To Reproduce

Exceptions (if any)

public class HostService1 : BackgroundService
    {
        private readonly ITestService _testService;
        public HostService1(ITestService testService)
        {
            _testService = testService;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _testService.SetValue(DateTime.Now.ToString());
            Console.WriteLine("BackgroundService ExecuteAsync!");
            await Task.CompletedTask;
        }
    }
public class TestService : ITestService
    {
        private string _value = "";

        public TestService()
        {
        }

        public string GetValue()
        {
            return _value;
        }

        public void SetValue(string value)
        {
            _value = value;
        }
    }
public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services
                .AddSingleton<ITestService, TestService>()
                .AddHostedService<HostService1>();
        }

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ...
            var testService = app.ApplicationServices.GetService<ITestService>();
            var value = testService.GetValue();

            testService.SetValue("testservice");
            Console.WriteLine("Setup Configure Execute!");
          ...
        }

11

Further technical details

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.20 [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.20 [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.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.20 [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.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

BrennanConroy commented 4 years ago

You should move your initialization code to either Main or ConfigureServices.

Tratcher commented 4 years ago

This is intentional. With the move to the generic host in 3.x the web service is now an IHostedService just like BackgroundService. Those run in the order they're registered. If you register your HostService1 in Program.cs before the web service you should see the order swap.

The delay to run Configure during StartAsync is also intentional. We found too many apps were running things like database migrations during Configure. There are tools that build the host to get access to services but they never start the host. Those tools did not want to run full app setup logic, only get the services.

ghost commented 4 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.