如何在 asp.net core 中获取 IConfiguration 的实例? [英] How to get an instance of IConfiguration in asp.net core?

查看:45
本文介绍了如何在 asp.net core 中获取 IConfiguration 的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个单元测试项目来测试我的 webapi,我需要初始化一个控制器,问题是它在构造函数中收到一个 IConfiguration,它由依赖注入提供并且工作正常.

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.

但是当我想手动初始化它时,我无法获得这个实例.

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.

控制器看起来像:

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

推荐答案

我可能应该从以下声明开始:在 .Net Core 应用程序中,您不应将 IConfiguration 的实例传递给您的控制器或其他班级.您应该使用通过 IOptions 注入的强类型设置.有关更多详细信息,请参阅本文:ASP.NET Core 中的选项模式.

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. You should use strongly typed settings injected through IOptions<T>. See this article for more details: Options pattern in ASP.NET Core.

使用选项模式时,您将拥有控制器所需设置的 POCO.然后将此设置对象注入到包裹在 IOptions 中的控制器中:

When using options pattern, you will have POCO for the settings required by a controller. This settings object is then injected into controller wrapped into 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;
    }
}

然后通过单元测试传递您想要的任何设置非常简单.只需填充设置实例并使用任何可用的模拟框架包装到 IOptions,例如 MoqNSubstitute:

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:

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

    //  ...
}

有时需要使用项目的真实配置,例如在开发集成测试时.在这种情况下,您可以创建 ConfigurationBuilder 的实例并使用与测试代码中相同的配置源填充它:

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:

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

这篇关于如何在 asp.net core 中获取 IConfiguration 的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆