.NET 6- IOPTIONS问题
过去,在.NET 3.1中,我编写了一个扩展方法(C#),我们在开发API时用来设置一些基本管道的东西。 其中之一是通过IOPTIONS注册某些配置类,因此我们可以将它们注入其他类,经理,控制器...
现在,我们切换到.NET 6,出于某种原因,同一件事不再工作,而且我无法弄清楚。
这是我在上述方法中的扩展方法
public static IServiceCollection AddDefaultApiSetup(
this IServiceCollection services, IConfiguration configuration)
{
//Method 1 I tried, from Microsoft Documentation:
// learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
services.Configure<HttpClientConfiguration>(
configuration.GetSection(Constants.HttpClientConfigurationKey));
//Method 2 I tried
var httpClientsConfig =
configuration.GetSection(Constants.HttpClientConfigurationKey)
.Get<HttpClientConfiguration>();
services.Configure<HttpClientConfiguration>(
Constants.HttpClientConfigurationKey,configuration);
return services.AddOtherStuff();
}
,使用方法2,httpclientsconfig变量确实包含正确的值,但是当在类中注入iOptions时,对象是空的。 使用方法1,它根本不起作用:)
如果我直接使用Builder.Services和Builder.configuration直接将代码直接移动到start.cs类中。因此,我必须做错了什么,而且我似乎找不到任何内容,因为所有网站似乎都在谈论.NET Core 3.1
当然,我可以在课堂中注入IconFiguration,然后阅读特定部分那里,但是我只是不喜欢那样,因为我只想拥有某些控制器中需要的信息,而不是整个配置。
有任何提示,建议,言论...? PS:它也无助于将iConfiguration更改为IconFigurationRoot或
更新: appSettings文件
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"HttpClients": {
"AppName": "My API",
"AppVersion": "1.0",
"Clients": [
{
"Name": "WebApiCore",
"BaseUri": "https://localhost:4520"
}
]
}
}
更新: 添加了我要在其中阅读配置中的课程。提醒“将配置读取为班级”有效,而不是通过ioptions方法...在我的课堂中注入iConFiguration,然后进行以下操作,但我想使用IOPTIONS
var config = _configuration.GetSection(Constants.HttpClientConfigurationKey).Get<HttpClientConfiguration>();
public class HttpClientConfiguration
{
public string AppName { get; set; } = string.Empty;
public string AppVersion { get; set; } = string.Empty;
public List<ApiConfiguration> Clients { get; set; } = new List<ApiConfiguration>();
}
In the past, in .NET 3.1 I wrote an extension method (C#) that we used to setup some basic pipeline stuff when developing an API.
One of these things was registration of some configuration classes via IOptions, so we could inject them into other classes, managers, controllers...
Now, we switched to .NET 6 and for some reason, the same thing is not working any longer, and I can't get it figured out.
This is my extension method
public static IServiceCollection AddDefaultApiSetup(
this IServiceCollection services, IConfiguration configuration)
{
//Method 1 I tried, from Microsoft Documentation:
// learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
services.Configure<HttpClientConfiguration>(
configuration.GetSection(Constants.HttpClientConfigurationKey));
//Method 2 I tried
var httpClientsConfig =
configuration.GetSection(Constants.HttpClientConfigurationKey)
.Get<HttpClientConfiguration>();
services.Configure<HttpClientConfiguration>(
Constants.HttpClientConfigurationKey,configuration);
return services.AddOtherStuff();
}
In the above method, using method 2, the httpClientsConfig variable does contain the correct values, but when injecting the IOptions in a class, the object is empty.
Using method 1, it just doesn't work at all :)
If I would move the code directly into the Startup.cs class directly using the builder.Services and builder.Configuration, it also doesn't work (d'uuuh), so I must be doing something wrong, and I can't seem to find anything about this, as all sites seem to be talking about .NET Core 3.1 examples
Of course, I could just inject IConfiguration in my classes and then read the specific section there, but I just don't like that, as I want to only have the info I need in some controllers, not the whole config.
Any tips, suggestions, remarks...?
PS: It also doesn't help changing IConfiguration to IConfigurationRoot or something
Update: appSettings file
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"HttpClients": {
"AppName": "My API",
"AppVersion": "1.0",
"Clients": [
{
"Name": "WebApiCore",
"BaseUri": "https://localhost:4520"
}
]
}
}
Update: added the class where I'm reading the configuration into. Remind that 'reading the configuration into the class' works, but not via the IOptions method... Injecting IConfiguration in my class and then doing the below just works, but I want to use IOptions
var config = _configuration.GetSection(Constants.HttpClientConfigurationKey).Get<HttpClientConfiguration>();
public class HttpClientConfiguration
{
public string AppName { get; set; } = string.Empty;
public string AppVersion { get; set; } = string.Empty;
public List<ApiConfiguration> Clients { get; set; } = new List<ApiConfiguration>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论