试图设置配置值"IHostBuilder",但未在Startup类中的ConfigureServices中反映出来-ASP.NET Core 3.1 [英] trying to set config value `IHostBuilder` but not reflected for ConfigureServices in Startup class - asp.net core 3.1

查看:84
本文介绍了试图设置配置值"IHostBuilder",但未在Startup类中的ConfigureServices中反映出来-ASP.NET Core 3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的配置中有空的连接字符串,

I have below config with empty connection string,

"ConnString": {
"Value":  "" 
},

现在在Asp.Net Core 3.1应用程序中,我正在尝试在 CreateHostBuilder

Now in Asp.Net Core 3.1 app I am trying to set the connection string value inside CreateHostBuilder

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureAppConfiguration((context, config) =>
                {
                    var settings = config.Build();
                    settings["ConnString:Value"] = "MY CONNECTION STRING";

                });

                webBuilder.UseStartup<Startup>();
            });

但是此值未反映在 Startup 类的 ConfigureServices 方法中,但仍显示为空.我应该在这里做什么?请提出建议!

But this value is NOT reflected in ConfigureServices method of Startup class, it is still showing empty. What should I need to do here? please suggest!

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
    {
        var updateValue = Configuration.GetSection("ConnString:Value").Value;
    }

推荐答案

您正在构建配置并在 new IConfiguration 对象上设置值.但是,由于您未在 builder 上设置值,因此这些设置将丢失.基础基础结构稍后再次调用 Build ,这将创建另一个 IConfiguration .这些值不会神奇地转移过来.

You are building the configuration and setting the value on the new IConfiguration object. However these settings will be lost as you aren't setting the values on the builder. The underlying infrastructure calls Build again later which creates another new IConfiguration. The values don't magically transfer over.

您可以做的是调用

What you can do instead is call AddInMemoryCollection and pass your new settings there. This will then get consumed by the infrastructure and be made available later (ie in ConfigureServices):

webBuilder.ConfigureAppConfiguration((context, config) =>
{
   config.AddInMemoryCollection(
     new [] { 
       new KeyValuePair<string, string>("ConnString:Value", "MY CONNECTION STRING") 
     }
   );
});

IWebHostBuilder 也具有

IWebHostBuilder also has a UseSetting method where you can configure a single setting. This adds a new in-memory configuration provider containing just one value:

webBuilder.UseSetting("ConnString:Value", "MY CONNECTION STRING");

请注意, IWebHostBuilder 将使用 IHostBuilder (通用主机)中的配置.您也可以在主机生成器上调用 ConfigureAppConfiguration .

Note that the IWebHostBuilder will consume the configuration from the IHostBuilder (the generic host). You could alternatively call ConfigureAppConfiguration on the host builder instead.

Host.CreateDefaultBuilder(args) 
    .ConfigureAppConfiguration((context, config) =>
     {
        config.AddInMemoryCollection(
          new [] { 
            new KeyValuePair<string, string>("ConnString:Value", "MY CONNECTION STRING") 
          }
        );
     });

此注射"操作如果您具有用于配置其他设置的设置,则该功能特别有用.您可以改为在 ConfigureHostConfiguration 中添加它们(对于 IWebHostBuilder 不存在),从而允许在内部 ConfigureAppConfiguration 中使用它们的值.code>在任何一个构建器上.

This "injection" is particularly useful if you have settings that are used to configure other settings. You could instead add them in ConfigureHostConfiguration (which doesn't exist for IWebHostBuilder) allowing their values to be available within ConfigureAppConfiguration on either builder.

请注意,配置没有后退共享给主机构建器.您添加到网络主机的任何配置都不会自动应用于通用主机.我之所以这样称呼是因为它与 ServiceCollection 的行为不同,后者在两者之间共享(向一个服务添加服务会添加到另一个服务).

Note that the configuration isn't shared back to the host builder. Any configuration you add to the web host doesn't automatically apply to the generic host. I call this out because it is different from the behavior of the ServiceCollection which is shared between the two (adding services to one adds to the other).

更新:

我在上面提到 ConfigureHostConfiguration .这对于配置Azure KeyVault之类的功能特别有用.

I mentioned above that ConfigureHostConfiguration can be used if you have settings used to configure/setup other configuration providers. This is particularly useful for something like configuring Azure KeyVault.

Host.CreateDefaultBuilder(args) 
    .ConfigureHostConfiguration(config =>
     {
        // setup bootstrapping settings. For example if we need to add in values used
        // to configure azure key vault stored in app settings and maybe some in memory 
        config.AddJsonFile("appsettings.json");
        config.AddInMemoryCollection(
          new [] { 
            new KeyValuePair<string, string>("ClientId", "AzureClientId"), 
            new KeyValuePair<string, string>("TenantId", "AzureTenantId") 
          }
        );
     })
     // settings added in ConfigureHostConfiguration are now available via context.Configuration
    .ConfigureAppConfiguration((context, config) =>
     {
         // use the values we added during bootstrapping 
         var tenant = context.Configuration["TenantId"];
         var client = context.Configuration["ClientId"];
         // example, I don't remember the parameters off-hand
         config.AddAzureKeyVault(tenant, client, /*etc*/);
     });

在此示例中,也可以改为在 IWebHostBuilder 上调用 ConfigureAppConfiguration .

In this example ConfigureAppConfiguration could also be called on the IWebHostBuilder instead.

有关配置和通用主机的更多信息,请请参阅

For more information about configuration and the generic host, see the documentation.

这篇关于试图设置配置值"IHostBuilder",但未在Startup类中的ConfigureServices中反映出来-ASP.NET Core 3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆