Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException
Posted
技术标签:
【中文标题】Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException【英文标题】: 【发布时间】:2021-12-06 14:35:52 【问题描述】:我正在使用 Asp.Net Core Web Api 6
我在迁移 DbContext 和更新数据库时遇到错误
错误
[17:07:29 INF] Application Is Starting
[17:07:29 FTL] Application terimnated unexpectedly
: Exception of type '' 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
但它不影响迁移过程和更新数据库
因此,我的问题是,
如何解决 这会影响以后的项目吗?程序.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.0000ms";
);
// 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 6 docs.microsoft.com/en-us/dotnet/core/compatibility/… 中的重大变化 【参考方案1】:在任何 .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
【讨论】:
以上是关于Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException的主要内容,如果未能解决你的问题,请参考以下文章