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

Hosting as a Windows Service #29825

Closed Manmohabe closed 3 years ago

Manmohabe commented 3 years ago

Describe the bug

Asp.net core 3.1 hosted as a windows service fails to start when there is no proper shutdown of windows. By this I mean when there is power interruption and the machine abruptly shuts down. Even although the application is configured to automatic start it fails to start you have to do a manual start. I haven't yet tested with 5.1

This is my Configuration

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseWindowsService() // used only in windows as a service
         .ConfigureWebHostDefaults(webBuilder =>
         {
             webBuilder.UseKestrel();
             webBuilder.UseUrls("http://localhost:5000");
             //Enable if you want to access on a LAN remotely
             //webBuilder.UseUrls("http://0.0.0.0:5000");
             webBuilder.UseStartup<Startup>();
         })
           .ConfigureServices(services =>
           {
               services.AddHostedService<TimedHostedService>();
           });

}

To Reproduce

Exceptions (if any)

Further technical details

.NET SDK (reflecting any global.json): Version: 5.0.101 Commit: d05174dc5a

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

Host (useful for support): Version: 5.0.1 Commit: b02e13abab

.NET SDKs installed: 3.1.403 [C:\Program Files\dotnet\sdk] 5.0.101 [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.23 [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.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

blowdart commented 3 years ago

Do you have any log files to show any errors on restart? Without that everything frankly is guess work.

Manmohabe commented 3 years ago

Do you have any log files to show any errors on restart? Without that everything frankly is guess work.

Thanks for your time, After analysis of the logs, the issue seam to originate from a transient error in the ef core (using pomelo as a database provider). At startup i had configured a method that does initialization of the database with some data. Check the code below

public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var serviceProvider = services.GetRequiredService<IServiceProvider>();
                    //var configuration = services.GetRequiredService<IConfiguration>();
                    InitialUserSetUp.CreateUsers(serviceProvider).Wait();
                }
                catch (Exception exception)
                {
                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(exception, "An error occurred while doing the initial setup");
                }
            }
            host.Run();
        }

After Configuring the Connection Resiliency, I have not been able to reproduce the issue.

    services.AddDbContextPool<ApplicationUserDbContext>(options =>
                options.UseMySql(ConnectionString,
                mySqlOptions =>
            mySqlOptions.EnableRetryOnFailure(
                maxRetryCount: 10,
                maxRetryDelay: TimeSpan.FromSeconds(30),
                errorNumbersToAdd: null)
        ));