【问题标题】:Accessing IOptions<T> object .NET Core访问 IOptions<T> 对象 .NET Core
【发布时间】:2021-07-14 20:54:47
【问题描述】:

我是 .NET Core 的新手,如果这是一个新手问题,我深表歉意。 我使用 VS 2017 在 .NET Core 2 中创建了一个 Web API 项目。

对我来说,我有带有一些连接设置的 appsettings.json 文件。

在阅读了来自微软教程的IOptions&lt;T&gt; 之后,我正在做类似以下的事情:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<MyOptions>(Configuration);

    // I want to access MyOptions object here to configure other services?how do i do that?

    service.AddHangfire( // Get MyOptions.HangfireConenctionString etc.)
}

如何在ConfigureServices 中访问创建的MYOptions 对象,以及是否需要在Configure(IApplicationBuilder app,..) 方法中访问它?

我只在教程中看到了控制器中注入IOptions&lt;T&gt; 的示例。

【问题讨论】:

    标签: c# dependency-injection asp.net-core


    【解决方案1】:

    使用一些设置

    public void ConfigureServices(IServiceCollection services)
    {
        // Load the settings directly at startup.
        var settings = Configuration.GetSection("Root:MySettings").Get<MySettings>();
    
        // Verify mailSettings here (if required)
    
        service.AddHangfire(
            // use settings variable here
        );
    
        // If the settings needs to be injected, do this:
        container.AddSingleton(settings);
    }
    

    如果您需要在应用程序组件中使用您的配置对象不要IOptions&lt;T&gt; 注入到您的组件中,因为这只会导致不幸的缺点,如here 所述。而是直接注入值,如下例所示。

    public class HomeController : Controller  
    {
        private MySettings _settings;
        public HomeController(MySettings settings)
        {
            _settings = settings;
        }
    }
    

    【讨论】:

    • 非常感谢@Steven
    • 比较 Steven 和@Frank Nielsen 的答案,推荐哪种启动方式?为什么?
    • 请不要就为什么IOptions&lt;T&gt; 是福音真理的错误答案发表意见。编程中几乎没有绝对性。
    【解决方案2】:

    你很亲密

    services.Configure<MyOptions>(options => Configuration.GetSection("MyOptions").Bind(options));
    

    您现在可以使用依赖注入访问您的 MyOptions

    public class HomeController : Controller  
    {
        private MySettings _settings;
        public HomeController(IOptions<MySettings> settings)
        {
            _settings = settings.Value
            // _settings.StringSetting == "My Value";
        }
    }
    

    我从这篇出色的文章中提取了 sn-ps:Andrew Lock 的https://andrewlock.net/how-to-use-the-ioptions-pattern-for-configuration-in-asp-net-core-rc2/

    【讨论】:

    • 由于声誉问题,我无法评论@Steven 的帖子,但他是关于注射的正确方法之一。感谢您的通知。
    【解决方案3】:

    这取决于您的appsettings.json 以及您如何命名该部分。例如:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "MySettings": {
        "ConnectionString": "post.gres:39484"
      }
    }
    

    这里,该部分的名称MySettings。通常,在我的Startup.cs 的 ASP.NET Core 2.2 中,我使用类似:

    services.AddOptions();
    services.Configure<MyOptions>(Configuration.GetSection("MySettings"));
    

    但类似的方法也有效:

    services.AddOptions();
    services.AddOptions<MyOptions>().Bind(Configuration.GetSection("MySettings"));
    

    如果您已将程序配置为使用AddEnvironmentVariables(),则还可以通过添加名为MySettings__ConnectionString 的配置变量来设置设置。 The value can come from multiple places.

    如果你想validate your options class you might want to use Data Annotations

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 2019-06-12
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多