gpt4 book ai didi

c# - 读取配置项并使用 `IOptions` 失败

转载 作者:行者123 更新时间:2023-12-05 04:50:18 24 4
gpt4 key购买 nike

我不明白!我发誓我严格按照文档 ( Options pattern in ASP.NET Core ) 进行操作,但是一旦我开始使用我的服务,选项值为 null。

这是 appsettings.json 文件内容;

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConfigStrings": {
"IDProofingQuestionsPath": "C:\\dev\\...\\wwwroot\\sample-data\\idProofingQuestionsMOCK.json"
}
}

(如您所知,此路径用于测试某些内容,不会永久存在。)

这是我的 ConfigureServices() 方法。

public void ConfigureServices(IServiceCollection services)
{
// IDProofingQuestionsPathOptions.IDProofingQuestionsPath <- Section:Option string for the Settings file.
services.Configure<IDProofingQuestionsPathOptions>(Configuration.GetSection(IDProofingQuestionsPathOptions.IDProofingQuestionsPath));

// This is the service into which I'm trying to inject this 'Option'
services.AddScoped<IIDProofingServices, IDProofingServices>();

services.AddDistributedMemoryCache();

services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});

services.AddControllersWithViews();
}

这是我的服务代码,我试图将 IDProofingQuestionsPathOptions 实例注入(inject)其中。

public class IDProofingServices : IIDProofingServices
{
private readonly string _proofingQuestionsPath;

/// <summary>
/// This is a parameterized constructor for allowing Dependency Injection
/// </summary>
public IDProofingServices(
IOptions<IDProofingQuestionsPathOptions> proofingQuestionsPath)
{
if (proofingQuestionsPath == null)
{
throw new ArgumentNullException(nameof(proofingQuestionsPath));
}

if (string.IsNullOrWhiteSpace(proofingQuestionsPath.Value.IdProofingQuestionsPath))
{
// my code ends up here, and I just do NOT get what I'm doing wrong.
throw new ArgumentNullException("proofingQuestionsPath.Value.IdProofingQuestionsPath");
}

_proofingQuestionsPath = proofingQuestionsPath.Value.IdProofingQuestionsPath;
}

...

哦,当然还有选项对象。

public class IDProofingQuestionsPathOptions
{
public const string IDProofingQuestionsPath = "ConfigStrings:IDProofingQuestionsPath";

public string IdProofingQuestionsPath { get; set; }
}

最佳答案

Configure方法需要一个配置部分 object,但是您对 GetSection 的调用只提供一个 string 值,没有任何可以绑定(bind)到选项对象的子项。

一个简单的修复方法是直接绑定(bind)到 ConfigStrings 属性,或者为您的路径属性引入一个 JSON 包装器。


后一种解决方案的最小化示例:

启动:

public void ConfigureServices(IServiceCollection services)
{
// Bind to the 'MyPathOptions' wrapper object
services.Configure<PathOptions>(Configuration.GetSection("ConfigStrings:MyPathOptions"));

// ...
}

路径选项:

public class PathOptions
{
public string MyPath { get; set; }
}

appsettings.json:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConfigStrings": {
"MyPathOptions": {
"MyPath": "abc"
}
}
}

测试 Controller :

[Route("")]
public class TestController : Controller
{
private readonly IOptions<PathOptions> _pathOptions;

public TestController(IOptions<PathOptions> pathOptions)
{
_pathOptions = pathOptions ?? throw new ArgumentNullException(nameof(pathOptions));
}

[HttpGet]
public IActionResult Index()
{
return Ok(_pathOptions);
}
}

关于c# - 读取配置项并使用 `IOptions<T>` 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67338776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com