即使GetSection正在运行,ServiceCollection也会为IOptions返回null

编程入门 行业动态 更新时间:2024-10-25 01:28:11
本文介绍了即使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

本文发布于:2023-11-14 19:13:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会为   正在运行   GetSection   ServiceCollection   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!