简体   繁体   English

如何在asp.net核心中获取IConfiguration的实例?

[英]How to get an instance of IConfiguration in asp.net core?

I making a unittesting project to test my webapi and i need to initialize a controller the problem is that in the constructor it receive a IConfiguration that it is provide by dependency-injection and works fine. 我做一个单元测试项目来测试我的webapi,我需要初始化一个控制器,问题是在构造函数中它收到一个IConfiguration,它是通过依赖注入提供的,并且工作正常。

But when I want to initialize it manually i have not way to get this instance. 但是当我想手动初始化它时,我无法得到这个实例。

I am trying to initialize it from a unittest project no inside of the same project. 我试图从同一个项目中的单元测试项目初始化它。

The controller looks like: 控制器看起来像:

public Controller(IConfiguration configuration) { _configuration = configuration; }

I'd probably should start from the statement that in .Net Core application you should not pass instance of IConfiguration to your controllers or other classes. 我可能应该从.Net Core应用程序的声明开始,你不应该将IConfiguration实例传递给你的控制器或其他类。 You should use strongly typed settings injected through IOptions<T> . 您应该使用通过IOptions<T>注入的强类型设置。 See this article for more details: Options pattern in ASP.NET Core . 有关更多详细信息,请参阅此文章: ASP.NET Core中的选项模式

When using options pattern, you will have POCO for the settings required by a controller. 使用选项模式时,您将获得控制器所需设置的POCO。 This settings object is then injected into controller wrapped into IOptions<T> : 然后将此设置对象注入到包含在IOptions<T>控制器中:

public class ControllerSettings
{
    public string Setting1 { get; set; }

    public int Setting2 { get; set; }

    // ...
}

public class Controller
{
    private readonly ControllerSettings _settings;

    public Controller(IOptions<ControllerSettings> options)
    {
        _settings = options.Value;
    }
}

Then it's pretty simple to pass any settings you want from a Unit Test. 然后从单元测试中传递您想要的任何设置非常简单。 Just fill settings instance and wrap to IOptions<T> with any of available mocking frameworks, like Moq or NSubstitute : 只需填充设置实例并使用任何可用的模拟框架(如MoqNSubstitute)包装到IOptions<T>

[TestMethod]
public void SomeTest()
{
    var settings = new ControllerSettings
    {
        Setting1 = "Some Value",
        Setting2 = 123,
    };

    var options = new Mock<IOptions<ControllerSettings>>();
    options.Setup(x => x.Value).Returns(settings);

    var controller = new Controller(options.Object);

    //  ...
}

Sometimes it's required to use real configuration of the project, for example when developing integration test. 有时需要使用项目的实际配置,例如在开发集成测试时。 In this case you could create instance of ConfigurationBuilder and fill it with the same configuration sources as in tested code: 在这种情况下,您可以创建ConfigurationBuilder实例并使用与测试代码中相同的配置源填充它:

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
// Duplicate here any configuration sources you use.
configurationBuilder.AddJsonFile("AppSettings.json");
IConfiguration configuration = configurationBuilder.Build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM