从 Startup 类访问 Configuration 对象

Access to Configuration object from Startup class(从 Startup 类访问 Configuration 对象)
本文介绍了从 Startup 类访问 Configuration 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 ASP.NET vNext 项目的许多控制器中访问我公司的 Active Directory,并且我将域名插入到我的 config.json 文件中,因此我可以从 Configuration 类中访问它.我发现每次我想访问我的 config.json 时总是实例化一个新的 Configuration 对象很重,有没有办法通过 IConfiguration API 访问初始化到 Startup 类中的 Configuration 类?

I would like to access to the Active Directory from my company in many controllers from my ASP.NET vNext project, and I inserted the domain name into my config.json file, so I can access it from the Configuration class. I find it heavy to always instantiate a new Configuration object at every time I want to access to my config.json, is there any way through the IConfiguration API to access to the Configuration class initialized into the Startup class ?

推荐答案

如何做到这一点的示例:

An example of how you can do this:

假设您有一个如下所示的 config.json:

Let's assume you have a config.json like below:

{
    "SomeSetting1": "some value here",
    "SomeSetting2": "some value here",
    "SomeSetting3": "some value here",
    "ActiveDirectory": {
        "DomainName": "DOMAIN-NAME-HERE"
    }
}

创建一个包含您的选项信息的 POCO 类型:

Create a POCO type having your option information:

public class ActiveDirectoryOptions
{
    public string DomainName { get; set; }
}

在 Startup.cs 中,配置服务时:

In Startup.cs, when configuring services:

services.Configure<ActiveDirectoryOptions>(optionsSetup =>
{
    //get from config.json file
    optionsSetup.DomainName = configuration.Get("ActiveDirectory:DomainName");
});

在所有想要获取此配置设置的控制器中,执行类似...这里的选项由 DI 系统注入:

In all controllers which want to get this config setting, do something like...Here the options is injected by the DI system:

public class HomeController : Controller
{
    private readonly IOptions<ActiveDirectoryOptions> _activeDirectoryOptions;

    public HomeController(IOptions<ActiveDirectoryOptions> activeDirectoryOptions)
    {
        _activeDirectoryOptions = activeDirectoryOptions;
    }

    public IActionResult Index()
    {
        string domainName = _activeDirectoryOptions.Options.DomainName;

        ........
    }
}

<小时>

回复评论:
我能想到几个选项:


Responding to the comment:
There are couple of options that I can think of:

  1. 在动作中,你可以做

  1. From within the action, you can do

var options = HttpContext.RequestServices.GetRequiredService

你可以有一个用FromServicesAttribute修饰的动作参数.该属性将导致从 DI 中检索参数值.示例:

You can have a parameter to the action which is decorated with FromServicesAttribute. This attribute will cause the parameter value to be retrieved from the DI. Example:

public IActionResult Index([FromServices] IOptions options)

我更喜欢 #2 而不是 #1,因为在单元测试的情况下,它会为您提供所有相关部分的信息.

I prefer #2 over #1 as in case of unit testing it gives you information on all dependent pieces.

这篇关于从 Startup 类访问 Configuration 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)