C# 访问IOptions<;T>;对象.NET内核

C# 访问IOptions<;T>;对象.NET内核,c#,dependency-injection,asp.net-core,C#,Dependency Injection,Asp.net Core,我是.NETCore的新手,如果这是一个新手问题,我深表歉意。 我使用VS2017在.NET Core 2中创建了一个Web API项目 对我来说,我有带有一些连接设置的appsettings.json文件 在阅读了microsoft教程中的IOptions之后,我做了如下工作: public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configur

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

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

在阅读了microsoft教程中的
IOptions
之后,我做了如下工作:

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.)
}
public void配置服务(IServiceCollection服务)
{
services.AddOptions();
服务。配置(配置);
//我想在这里访问MyOptions对象以配置其他服务?我该怎么做?
service.AddHangfire(//获取MyOptions.HangfireConenctionString等)
}
如何在
ConfigureServices
中访问已创建的
MYOptions
对象,以及是否需要在
Configure(IApplicationBuilder应用程序,…)
方法中访问它

在教程中,我只在控制器中看到注入
IOptions
的示例

你很接近了

services.Configure<MyOptions>(options => Configuration.GetSection("MyOptions").Bind(options));
services.Configure(选项=>Configuration.GetSection(“MyOptions”).Bind(选项));
您现在可以使用依赖项注入访问MyOptions

public class HomeController : Controller  
{
    private MySettings _settings;
    public HomeController(IOptions<MySettings> settings)
    {
        _settings = settings.Value
        // _settings.StringSetting == "My Value";
    }
}
公共类HomeController:控制器
{
私人MySettings\u设置;
公共家庭控制器(IOPS设置)
{
_设置=设置。值
//_settings.StringSetting==“我的值”;
}
}

我从Andrew Lock的这篇优秀文章中摘录了一些片段。

在中使用一些设置

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);
}

这取决于您的
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.Configure(Configuration.GetSection(“MySettings”);
但类似的方法也能奏效:

services.AddOptions();
services.AddOptions<MyOptions>().Bind(Configuration.GetSection("MySettings"));
services.AddOptions();
services.AddOptions().Bind(Configuration.GetSection(“MySettings”);
如果已将程序配置为使用
AddEnvironmentVariables()
,则还可以通过添加名为
MySettings\uuu ConnectionString
的配置变量来设置


如果你愿意的话。

由于声誉问题,我不能评论@Steven post,但他在注射方面是正确的。谢谢通知。非常感谢@Steven比较Steven和@Frank Nielsen的答案,推荐哪种启动方法?为什么呢?这篇文章在回答中的链接部分描述了注入
IOptions
的缺点,该部分被移至
services.AddOptions();
services.AddOptions<MyOptions>().Bind(Configuration.GetSection("MySettings"));