【问题标题】:aspnet core dependency injection with IOptions in startup在启动时使用 IOptions 进行 aspnet 核心依赖注入
【发布时间】:2020-10-28 18:19:37
【问题描述】:

帮派怎么了?

我正在寻找一种使用 IOptions 包将配置注入我的类的正确且干净的方法。目前,我将所有配置对象注册在一个地方,一切都很好。问题在于我需要在启动方法中注册的一些类。

初始方法实现如下:(扩展方法)

public static void RegisterHttpClients(this IServiceCollection services, AppSettings appSettings)
{
    services.AddHttpClient<IPdfService, PdfService>(
        httpClient => httpClient.DefaultRequestHeaders.Add("PdfApiKey", appSettings.PdfApiKey));
}

这个 AppSettings 对象我也使用 IOptions 注入到其他类中,所以想以某种方式从那里重用该对象,而不是像这样再次从 json 中获取相同的对象:

var appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();
services.RegisterHttpClients(appSettings);

换句话说 - 我已经在 services.Configure&lt;AppSettings&gt;(configuration.GetSection("AppSettings")); 注册了这个东西,所以如果可能的话,我想搭载它。

关于如何配置它的可能方向的任何想法/建议?

编辑:(添加 JSON 和 AppSettings 文件)

public class AppSettings 
{
    public string PdfApiKey {get; set;}
    public string AnotherProperty {get; set;}
}
{
    "AppSettings": {
        "PdfApiKey": "SomeKey",
        "AnotherProperty": "HollyTheCow"
    }
}

提前致谢!

【问题讨论】:

  • 显示你的AppSettings类和json
  • 已添加。问题是应用程序的其余部分可以很好地获取设置,因此配置解析肯定是正确的。

标签: asp.net-core dependency-injection


【解决方案1】:

您可以通过使用 Configure 注册您的选项(您已经完成)来完成此操作,然后使用 AddHttpClient() 的第二个重载从 DI 容器(服务定位器模式)中获取注册的选项。注意我使用了IServiceProvider.GetRequiredService,但如果它是可选设置,你可以使用IServiceProvider.GetService()?.Value

services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));

services.AddHttpClient<YourService>((serviceProvider, config) => {
    var settings = serviceProvider.GetRequiredService<IOptions<AppSettings>>().Value;

    config.BaseAddress = new Uri(settings.BaseUri);
});

【讨论】:

    【解决方案2】:

    您的代码没有任何错误,将成功运行

    您的项目中可能有很多appSetting.json 文件。

    检查您项目中的ASPNETCORE_ENVIRONMENT变量。

    右键单击您的项目 > 属性 > 调试 > 环境变量

    如果ASPNETCORE_ENVIRONMENT 设置为开发 IConfiguration 从以下位置读取数据 appSettings.development.json 你应该在appSettings.development.json 中添加AppSettigs json

    如果ASPNETCORE_ENVIRONMENT 设置为生产 IConfigurationappSettings.production.json 你应该在appSettings.production.json 中添加AppSettigs json

    如果你想 IConfigurationappSetting.json 读取数据,请删除 appSettings.development.jsonappSettings.production.json 文件.

    在这种情况下 IConfiguration 默认从 appSetting.json

    读取和绑定数据

    其他选项

    您可以配置您的IConfiguration 以从动态加载数据 appSettings.json这样的

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

    【讨论】:

    • 这很酷。谢谢。事情仍然有点不清楚如何避免对 json 进行二次调用以进行配置(不喜欢在几个地方硬编码的“AppSettings”字符串)。在这种情况下,在 RegisterHttpClients 中,我必须传入 AppSettings,我只通过选项绑定进行注册。所以基本上我在一个地方有 services.Configure(configuration.GetSection("AppSettings")) 和 var appSettings = Configuration.GetSection("AppSettings").Get();在另一个(请注意,我必须使用两次“AppSettings”字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2016-12-07
    • 2018-01-03
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多