Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException
Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException
我正在使用 Asp.Net Core Web Api 6
我在迁移 DbContext 和更新数据库时遇到错误
错误
[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
但不影响Migration进程和Update-Database
因此,我的问题是,
- 如何修复
- 这对以后的项目有影响吗?
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();
在任何 .NET/EF Core 6.0 RC2 项目中添加类似于上述 IHostBulder.Build()
的 try/catch,并尝试添加迁移可以重现该问题。
我们可以通过以下方式解决问题:
catch (Exception ex)
{
string type = ex.GetType().Name;
if (type.Equals("StopTheHostException", StringComparison.Ordinal))
{
throw;
}
_logger.Fatal(ex, "Unhandled exception");
return 1;
}
有关此问题的更多详细信息,您可以参考这篇文章。
StopTheHostException should be made public to be dealt with gracefully #60600
我正在使用 Asp.Net Core Web Api 6
我在迁移 DbContext 和更新数据库时遇到错误
错误
[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
但不影响Migration进程和Update-Database
因此,我的问题是,
- 如何修复
- 这对以后的项目有影响吗?
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();
在任何 .NET/EF Core 6.0 RC2 项目中添加类似于上述 IHostBulder.Build()
的 try/catch,并尝试添加迁移可以重现该问题。
我们可以通过以下方式解决问题:
catch (Exception ex)
{
string type = ex.GetType().Name;
if (type.Equals("StopTheHostException", StringComparison.Ordinal))
{
throw;
}
_logger.Fatal(ex, "Unhandled exception");
return 1;
}
有关此问题的更多详细信息,您可以参考这篇文章。
StopTheHostException should be made public to be dealt with gracefully #60600