【问题标题】:.Net core not reading appsettings after publish.Net 核心在发布后不读取应用设置
【发布时间】:2018-04-10 17:49:42
【问题描述】:

很快: 我的创业课程看起来像这样:

  public Startup(IHostingEnvironment env)
    {
        this.env = env;
        var builder = new ConfigurationBuilder()
          .SetBasePath(env.ContentRootPath)
          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
          .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.RespectBrowserAcceptHeader = true;
            options.FormatterMappings.SetMediaTypeMappingForFormat(
                "xml", MediaTypeHeaderValue.Parse("text/xml"));
            options.FormatterMappings.SetMediaTypeMappingForFormat(
                "json", MediaTypeHeaderValue.Parse("application/json"));
        })
        .AddXmlSerializerFormatters();

        var dbConfig = Configuration.GetSection(nameof(DbOptions)).Get<DbOptions>();
        services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(dbConfig.ConnectionString));        
    }

并且: 虽然我在本地主机上一切正常,但是当我发布到网络时,应用程序会在每个请求上抛出 Status: 500 Internal server error。我想知道如果我更换

    var dbConfig = Configuration.GetSection(nameof(DbOptions)).Get<DbOptions>();
    services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(dbConfig.ConnectionString));   

    services.AddDbContext<GamesDbContext>(opt => opt.UseSqlServer(@"connectionstring"));   

它在本地和外部都像魅力一样工作,所以我猜问题在于 appsettings.js 的解析,但它有什么问题? appsettings.Development.json 基本上只是 appsettings.json 的复制粘贴

【问题讨论】:

  • 你能在连接字符串密码之后分享你的 appSettings 吗?
  • 您收到什么错误?
  • @Rabur 要获得确切的错误,您可以在应用程序设置中添加这两个设置ASPNETCORE_DETAILEDERRORS = true 和ASPNETCORE_ENVIRONMENT = Development。当您使用 Postman 访问 APi 时,这将为您提供详细的错误信息。等
  • @ShaswatRungta@HenkMollema 错误说:ArgumentNullException 参数名称:connectionString Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName) Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(string value, string parameterName) Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, string connectionString, Action sqlServerOptionsAction) GamesBoard.Startupc__DisplayClass9_0.b__1(DbContextOptionsBuilder opt) in Startup.cs
  • @AdemCatamak 它基本上看起来像这样:{ ... "DbOptions": { "ConnectionString": "Server=mssql.SERWERPATH;Database=youknow;Uid=myUsername;Password=SecretStuff;", "DefaultSchema": "gb" }, ... }

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


【解决方案1】:

当我阅读代码时,我认为您的 appSetting.Development.json 结构看起来像这样

{
    "DbOptions": {
         "connectionstrings": ""
     }
}

但是,您的 appSetting.json 结构看起来像这样

{
     "connectionstrings": ""
}

我建议这样使用

{
    "ConnectionStrings": {
        "DataContext": "Server=db;database=ProjectDb;uid=AppUser;pwd=AppPass;"
    },
    "Console": {
        "LogLevel": {
            "Default": "Warning"
         }
    }
}

你可以像这样轻松使用。

string sqlConnectionString = Configuration.GetConnectionString("DataContext");
services.AddDbContext<DataContext>(options => options.UseSql(sqlConnectionString));

在 Microsoft.Extensions.Configuration 类中具有读取 ConnectionStrings 的扩展名

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多