- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在手动构造一个IServiceProvider
时遇到了麻烦,它将允许我的单元测试使用它来使用GetService<IOptions<MyOptions>>
引入共享测试配置
我创建了一些单元测试来说明我的问题,如果可以用于回答问题,则此存储库也可以是found here。
JSON
{
"Test": {
"ItemOne": "yes"
}
}
public class TestOptions
{
public string ItemOne { get; set; }
}
ConfigureWithBindMethod
和
ConfigureWithBindMethod
均失败,并且
SectionIsAvailable
通过。因此,据我所知,该部分正按预期从JSON文件中使用。
[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")
时,我得到了这个值
{Microsoft.Extensions.Configuration.ConfigurationSection}
Key: "Test"
Path: "Test"
Value: null
最佳答案
要在服务集合中使用选项,您需要添加使用选项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);
}
关于.net - 即使GetSection正在运行,ServiceCollection也会为IOptions返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46019988/
我可以将 IConfiguration 配置注入(inject)构造函数,然后通过 config["settignName"] 从 json 文件访问应用程序设置; 服务类中的示例代码: public
我已经实现了 IndexBuilderSettings,它是某些服务的模型,例如当我为其创建一个模型时,如下所示 services.AddSingleton() .Configure((set
我已经实现了 IndexBuilderSettings,它是某些服务的模型,例如当我为其创建一个模型时,如下所示 services.AddSingleton() .Configure((set
是否可以将 JSON 文件 (appsettings.json) 中的属性绑定(bind)到使用不同属性名称的类? { "WebTarget": { "WebURL": "http:/
在 ASP.NET Core 2.1 中,我有一个使用 IOptions 接口(interface)发送电子邮件的类,如下所示: public class Email { private re
在我的 .NET Core 项目中,我在 Configure 方法中有以下设置: public void ConfigureServices(IServiceCollection services)
我正在尝试在我创建的服务中注入(inject) IOptions,但它始终为 null: public class Startup { private IConfiguration confi
在我看来,让域服务需要 IOptions 的实例是个坏主意。传递它的配置。现在我必须将额外的(不必要的?)依赖项添加到库中。我见过很多注入(inject) IOptions 的例子遍布网络,但我看不到
我正在尝试创建一个使用 IOptions 的通用方法作为参数,但代码似乎无效。 Visual Studio 显示以下消息: TConfig must be a non-abstract type wi
我有一个 worker service ind dotnet core 3.1: 程序.cs: public class Program { public static voi
我正在使用我在实际应用程序中使用的相同 Autofac 模块注册编写集成测试。我使用这样的基类将其连接起来: public abstract class ContainerFixture where
我不明白!我发誓我严格按照文档 ( Options pattern in ASP.NET Core ) 进行操作,但是一旦我开始使用我的服务,选项值为 null。 这是 appsettings.jso
选项模式允许我创建包含配置值的选项对象,如下所述:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/op
我正在尝试在一个带有 NancyFX 的项目上实现选项模式(如推荐的 here )/TinyIOC但它不起作用。 我正在 Startup.cs.ConfigureServices 上注册选项方法,但是
我有一个从 ASP.Net Core(目标 4.5)和 4.5 网络应用程序引用的类库,我想共享应用程序设置。我在 4.5 端使用 Unity for DI,在 Core 端使用 Core Boots
我正在使用 IOptions<>根据 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
我想让类保持静态。是否有任何解决方法来注入(inject) IOptions 不修改访问修饰符? public static class Encrypter { private static
我正在尝试为一个类(在 .net Core 项目中)编写一个 xunit 测试,它看起来像: public Class FoodStore:IFoodStore { FoodList food
我刚开始使用 net core 2.1 构建 API。 我在 appsettings.json 中添加了我的连接字符串,我想访问它。 appsettings.json "MySettings":
我试图在使用内置 DI 机制的同时获得与 ASP.NET Core 2.1 应用程序一致的 .NET Framework 类库。现在,我创建了一个配置类并将适当的部分添加到 appsettings.j
我是一名优秀的程序员,十分优秀!