简体   繁体   English

在 configuration.build 之前从 appsettings.json 读取值

[英]Read values from appsettings.json before configuration.build

Is that possible to read values from appsettings.json before configuration.build?是否可以在 configuration.build 之前从 appsettings.json 读取值?

i need to get endpoint, clientId, secret and tenant to bind configuration with azure app configuration我需要获取端点、clientId、secret 和租户以将配置与 azure 应用程序配置绑定

Configuration = builder.AddAzureAppConfiguration(options =>
           {
              //here i need to get some values from appsettings.json

           }).Build()

I'm not sure about this, as I usually use the WebHostBuilder, but I think it should be something like:我不确定这一点,因为我通常使用 WebHostBuilder,但我认为它应该是这样的:

var settings = builder.Build();
Configuration = builder.AddAzureAppConfiguration(options =>
{
    options.Connect(settings["ConnectionStrings:AppConfig"])
}).Build();

edit: note I assumed you already added appsettings.json to the builder.编辑:注意我假设您已经将 appsettings.json 添加到构建器中。 If not, you need to add如果没有,您需要添加

var env = [something like]hostingContext.HostingEnvironment;

config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

You can use a ConfigurationBuilder as such:您可以像这样使用ConfigurationBuilder

var builder = new ConfigurationBuilder()
                    .SetBasePath("yourJsonPath")
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var config = builder.Build();

then you can retrieve the value you need:然后你可以检索你需要的值:

config.GetSection("AppConfig:Endpoint").Value

or just要不就

 config.GetSection("ClientId").Value

Depending on how your JSON is built.取决于您的 JSON 的构建方式。

It's not possible.这是不可能的。

Here are two examples of a scenario similar to yours in the Azure App Configuration GitHub examples:以下是 Azure 应用程序配置 GitHub 示例中与您的场景类似的两个示例:

Console Application控制台应用程序

https://github.com/Azure/AppConfiguration-DotnetProvider/blob/e227b0b454370751c2ddbebb143fd6e02a07c47b/examples/ConsoleApplication/Program.cs#L36 https://github.com/Azure/AppConfiguration-DotnetProvider/blob/e227b0b454370751c2ddbebb143fd6e02a07c47b/examples/ConsoleApplication/Program.cs#L36

The example shows that the ConfigurationBuilder is built to obtain an intermediate IConfiguration that is then used to provide a connection string for adding Azure App Configuration to the builder.该示例显示,构建ConfigurationBuilder是为了获取中间IConfiguration ,然后使用该中间 IConfiguration 提供连接字符串,以将 Azure 应用程序配置添加到构建器。

ASP.NET Core Web Application ASP.NET 内核 Web 应用

https://github.com/Azure/AppConfiguration-DotnetProvider/blob/e227b0b454370751c2ddbebb143fd6e02a07c47b/examples/ConfigStoreDemo/Program.cs#L26 https://github.com/Azure/AppConfiguration-DotnetProvider/blob/e227b0b454370751c2ddbebb143fd6e02a07c47b/examples/ConfigStoreDemo/Program.cs#L26

In the ASP.NET Core web application example the same pattern is used with the framework provided IConfigurationBuilder在 ASP.NET Core web 应用示例中,相同的模式与框架提供的IConfigurationBuilder一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM