首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException

Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException
EN

Stack Overflow用户
提问于 2021-12-06 14:35:52
回答 1查看 4.6K关注 0票数 17

我正在使用Asp.Net Core 6

在迁移我的DbContext和更新数据库时,我面临一个错误

误差

代码语言:javascript
复制
[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

但它不影响迁移过程和更新-数据库

所以,我的问题是,

  • 如何修复它
  • 这会影响以后的项目吗?

Program.cs

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-07 08:12:52

在任何.NET/EFCore6.0 RC2项目中添加类似于上述IHostBulder.Build()的try/catch,并且尝试添加迁移可以重现这个问题。

我们可以用以下方法解决这个问题:

代码语言:javascript
复制
catch (Exception ex)
{
   string type = ex.GetType().Name;
   if (type.Equals("StopTheHostException", StringComparison.Ordinal))
   {
      throw;
   }

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

有关此问题的更多详细信息,请参阅本文.

StopTheHostException应该被公开,以便被优雅地对待#60600

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70247187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档