即使GetSection正在运行,ServiceCollection也会为IOptions返回null [英] ServiceCollection returns null for IOptions even though GetSection is working

查看:64
本文介绍了即使GetSection正在运行,ServiceCollection也会为IOptions返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在手动构造 IServiceProvider 时遇到了麻烦,这将允许我的单元测试使用它来使用 GetService< IOptions< ; MyOptions>>>

I'm having trouble manually constructing a IServiceProvider that will allow my unit tests to use it to pull in shared test configuration using GetService<IOptions<MyOptions>>

我创建了一些单元测试来说明我的问题,为此的回购可以是在这里找到,如果对回答问题有用。

I created some unit tests to illustrate my problems, also the repo for this can be found here if it's useful in answering the question.

JSON

{
  "Test": {
    "ItemOne":  "yes"
  }
}

选项类

public class TestOptions
{
    public string ItemOne { get; set; }
}

测试

在这些测试中, ConfigureWithBindMethod ConfigureWithBindMethod 均失败,其中 SectionIsAvailable 通过。因此,据我所知,该部分正按预期从JSON文件中使用。

Out of these tests ConfigureWithBindMethod and ConfigureWithBindMethod both fail, where SectionIsAvailable passes. So the section is being consumed as expected from the JSON file as far as I can tell.

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void ConfigureWithoutBindMethod()
    {
        var collection = new ServiceCollection();

        var config = new ConfigurationBuilder()
            .AddJsonFile("test.json", optional: false)
            .Build();

        collection.Configure<TestOptions>(config.GetSection("Test"));

        var services = collection.BuildServiceProvider();

        var options = services.GetService<IOptions<TestOptions>>();

        Assert.IsNotNull(options);
    }

    [TestMethod]
    public void ConfigureWithBindMethod()
    {
        var collection = new ServiceCollection();

        var config = new ConfigurationBuilder()
            .AddJsonFile("test.json", optional: false)
            .Build();

        collection.Configure<TestOptions>(o => config.GetSection("Test").Bind(o));

        var services = collection.BuildServiceProvider();

        var options = services.GetService<IOptions<TestOptions>>();

        Assert.IsNotNull(options);
    }

    [TestMethod]
    public void SectionIsAvailable()
    {
        var collection = new ServiceCollection();

        var config = new ConfigurationBuilder()
            .AddJsonFile("test.json", optional: false)
            .Build();

        var section = config.GetSection("Test");
        Assert.IsNotNull(section);
        Assert.AreEqual("yes", section["ItemOne"]);
    }
}

指出可能有用

在立即窗口中调用 config.GetSection( Test)时,我会得到此值

When calling config.GetSection("Test") in the immediate window, I get this value

{Microsoft.Extensions.Configuration.ConfigurationSection}
    Key: "Test"
    Path: "Test"
    Value: null

在表面值上,我假设Value不应为null,让我以为我可能在这里缺少明显的东西,因此,如果有人能发现我在做错的事情,那将是天才。

At face value I'd have assumed Value should not be null, which is leading me to think I may be missing something obvious here, so if anyone can spot what I'm doing wrong that'd be genius.

谢谢!

推荐答案

要在服务集合中使用选项,您需要添加使用选项 collection.AddOptions( );

To use options in your service collection, you need to add the service required for using options collection.AddOptions();

这应该可以解决问题:

[TestMethod]
public void ConfigureWithoutBindMethod()
{
    var collection = new ServiceCollection();
    collection.AddOptions();

    var config = new ConfigurationBuilder()
        .AddJsonFile("test.json", optional: false)
        .Build();

    collection.Configure<TestOptions>(config.GetSection("Test"));

    var services = collection.BuildServiceProvider();

    var options = services.GetService<IOptions<TestOptions>>();

    Assert.IsNotNull(options);
}

这篇关于即使GetSection正在运行,ServiceCollection也会为IOptions返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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