在starup.cs配置中访问IHttpContextAccessor使我的应用程序崩溃

时间:2019-11-19 19:26:46

标签: c# asp.net-core asp.net-core-2.2

在我的startup.cs配置中,我需要获取域名。 我通过在IConfig中注入IHttpContextAccessor来做到这一点:

public void ConfigureServices(IServiceCollection services)
{

    [...]

    services.AddHttpContextAccessor();

    [...]

}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor httpContext, DBContext dbContext)
{

    [...]

    var host = httpContext.HttpContext.Request.Host.Host;
    var country = dbContext.Countries.FirstOrDefault(x => x.Domain == host);
    /*
    var cultureInfo = new CultureInfo(country.CultureInfo);
    cultureInfo.NumberFormat.CurrencySymbol = country.CurrencySymbol;
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    */

    [...]

    app.UseMvc();

}

如果我删除了var host = httpContext.HttpContext.Request.Host.Host;,则应用程序可以正常运行,但是如果存在,则该应用程序的构建会在VS Mac中中断(dotnet崩溃)。

HttpContext似乎为空,因为var host = httpContext.HttpContext?.Request.Host.Host;不会使应用程序崩溃。

我想念什么?

0 个答案:

没有答案