Home > database >  Microsoft.Extensions.Hosting.HostFactoryResolver HostingListener StopTheHostException
Microsoft.Extensions.Hosting.HostFactoryResolver HostingListener StopTheHostException

Time:12-08

I'm using Asp.Net Core Web Api 6

I'm facing an error when migrating my DbContext and when updating the database

The Error

[17:07:29 INF] Application Is Starting
[17:07:29 FTL] Application terimnated unexpectedly
Microsoft.Extensions.Hosting.HostFactoryResolver HostingListener StopTheHostException: Exception of type 'Microsoft.Extensions.Hosting.HostFactoryResolver HostingListener StopTheHostException' was thrown.
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value)
   at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in C:\...\Program.cs:line 44

But it does not affect the Migration process and Update-Database

Therefore, My question is,

  • How to fix it
  • Is this going to affect the project later?

Program.cs

Log.Information("Application Is Starting");

    var builder = WebApplication.CreateBuilder(args);

    // Full setup of serilog. We read log settings from appsettings.json
    builder.Host.UseSerilog((context, services, configuration) => configuration
        .ReadFrom.Configuration(context.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext());


    // Add services to the container.
    builder.Services.AddDbContext<LocalDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("localConnection"))
    );

    builder.Services.AddControllers();
    builder.Services.AddCors(options =>
    {
        options.AddPolicy("AllowAll", builder =>
            builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
            );
    });
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    var app = builder.Build();

    app.UseSerilogRequestLogging(configure =>
    {
        configure.MessageTemplate = "HTTP {RequestMethod} {RequestPath} ({UserId}) responded {StatusCode} in {Elapsed:0.0000}ms";
    });

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }

    app.UseCors("AllowAll");

    app.UseHttpsRedirection();

    app.UseAuthorization();

    app.MapControllers();

    app.Run();

CodePudding user response:

Add try/catch similar to the above around IHostBulder.Build() in any .NET/EF Core 6.0 RC2 project, and attempt to add a migration can reproduce the issue.

We can fix the issue with the following :

catch (Exception ex)
{
   string type = ex.GetType().Name;
   if (type.Equals("StopTheHostException", StringComparison.Ordinal))
   {
      throw;
   }

   _logger.Fatal(ex, "Unhandled exception");
   return 1;
}

For more details on this issue, you can refer to this article.

StopTheHostException should be made public to be dealt with gracefully #60600

  • Related