在我的Azure中,我有ENVIRONMENT = Development
,但我的设置没有加载。
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
.Build();
但它始终使用默认设置。
请更新您的以下行:
.SetBasePath(env.ContentRootPath)
这就是我在部署到 Azure 时配置启动.cs
public Startup(
IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var builder = new ConfigurationBuilder();
if (_hostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
else
{
builder
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
}
更新:在程序中尝试此代码.cs
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;
public Program(
IConfiguration configuration,
IHostingEnvironment hostingEnvironment)
{
_configuration = configuration;
_hostingEnvironment = hostingEnvironment;
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
}
如果您仍有任何问题,请告诉我
在我的程序中.cs我做到了
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
.Build();
比我做的
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args) // Sets up the default order in which all the configurations are read
.UseStartup<Startup>()
.ConfigureAppConfiguration((c, x) =>
{
x.AddConfiguration(Configuration); <-------
})
.UseSerilog((h, c) => c.Enrich.FromLogContext().WriteTo.Sentry(s =>
{
s.Dsn = new Sentry.Dsn(Configuration.GetSection("Sentry:Dsn").Value);
s.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
s.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Information;
})).UseSentry(x =>
{
x.IncludeRequestPayload = true;
});