[英].Net Core 3.1 adding additional config.json file to configuration argument in Startup
I need to add another configuration file to my .Net Core Blazor project.我需要向我的 .Net Core Blazor 项目添加另一个配置文件。 Other sources (such as this ) mention using configuration builder in .Net core 2.2, but since in 3.1 the configuration is passed as a parameter into Startup, I have no way of appending another config onto it as far as I can tell.
其他来源(例如this )提到在 .Net core 2.2 中使用配置构建器,但由于在 3.1 中,配置作为参数传递给 Startup,据我所知,我无法将另一个配置附加到它上面。 Maybe I'm looking at it wrong, but I'm assuming that the configuration passed in as a param has important configuration properties, therefor building it my self with just the additional config file seems like it leave those config properties out.
也许我看错了,但我假设作为参数传入的配置具有重要的配置属性,因此仅使用附加配置文件自行构建它似乎将这些配置属性排除在外。
Ex: Doesn't work but should paint a picture of what I'm trying to do.例如:不起作用,但应该描绘出我正在尝试做的事情。
public Startup(IConfiguration configuration)
{
Configuration = configuration;
var builder = new ConfigurationBuilder(configuration)
.AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true);
});
}
You can do it in Program.cs
ie earlier in the pipeline rather than in Startup.cs
.您可以在
Program.cs
执行此操作,即在管道中的较早位置而不是在Startup.cs
。
Example:例子:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, config) =>
{
var env = hostContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Your approach should work, but needs a little bit of tweaking.您的方法应该可行,但需要稍作调整。
The configuration that you create needs to be added to the DI Container.您创建的配置需要添加到 DI 容器。 The documentation for this is here .
这方面的文档是here 。
I've achieved it as well in Azure Functions and other projects through the following:我也通过以下方式在 Azure Functions 和其他项目中实现了它:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(provider => new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true)
.Build());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.