英文:
Blazor WASM CS1503: Cannot convert from IConfigurationSection to Action<T>
问题
在ASP.NET应用程序中,我通过以下方式访问appsettings.json
中的通用配置部分,例如:
builder.Services.Configure<FileUploadChecksModel>(builder.Configuration.GetSection("UploadChecks"));
其中,FileUploadChecksModel
是一个简单的模型类,与 appsettings.json
中的相应部分匹配。
在Blazor WASM项目中,我可以将 appsettings.json
放在 wwwroot
中以相同的方式使用它,但是上面的代码行会引发以下错误:
CS1503 参数 2:无法将 'Microsoft.Extensions.Configuration.IConfigurationSection' 转换为 'System.Action<Models.FileUploadChecksModel>'
原来,builder.Services.Configure()
方法在其他ASP.NET应用程序中期望一个IConfiguration
作为参数,而在Blazor WASM项目中期望一个Action<>
。
问题是为什么在这里会有这种不同,以及在这种情况下如何最好地将数据传递给我的模型?
英文:
I'm wondering about the following:
In ASP.NET applications I access general configuration sections from appsettings.json
as follows for example:
builder.Services.Configure<FileUploadChecksModel>(builder.Configuration.GetSection("UploadChecks"));
Where FileUploadChecksModel
is a simple model class, which matches the respective section in appsettings.json
In a Blazor WASM project I can put appsettings.json
in wwwroot
to use it in the same way, however the same line of code as above throws the following error:
> CS1503 Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action<Models.FileUploadChecksModel>'
Turns out the method builder.Services.Configure()
expects an IConfiguration
in other ASP.NET applications and Action<>
in a Blazor WASM project as an argument.
Question is why this is different here and how to best get the the data into my model in this case?
答案1
得分: 1
在文档中找到了答案 in the documentation.
只需将 Microsoft.Extensions.Options.ConfigurationExtensions
nuget 包添加到 Blazor WASM 项目中。
英文:
Found the answer in the documentation.
One simply needs to add the Microsoft.Extensions.Options.ConfigurationExtensions
nuget package to the Blazor WASM project.
答案2
得分: 1
使用IConfiguration.Get<>
方法,这是一个扩展方法。
假设我想加载一个名为ChatSettings.cs的C#模型:
public class ChatSettings
{
public string BaseUrl { get; set; }
public string Scope { get; set; }
}
我会更新我的appsettings,使其如下:
{
...
"Chat": {
"BaseUrl": "https://localhost:7040/",
"Scope": "api://xxxx-xxx-xxxx-xxxx-xxxxxx/access_as_user"
}
...
}
然后创建一个用于appsettings.json根的模型:
public class LocalConfigurations
{
public ChatSettings Chat { get; set; }
}
在program.cs中:
var config = builder.Configuration.Get<LocalConfigurations>();
如果你希望从另一个服务或Razor组件中访问这些信息,添加以下内容:
builder.Services.AddSingleton(_ => config);
在一个服务中,只需将它添加到构造函数中:
public class SomeService
{
public SomeService(LocalConfigurations config)
{
ChatSettings chatConfig = config.Chat;
}
}
在一个.razor文件中,可以使用@inject
或[Inject]
属性来访问属性:
@inject LocalConfigurations Config
...
@code {
[Inject]
private LocalConfigurations Config { get; set; }
}
只使用一种注入方法。我更喜欢使用属性方式,因为我更倾向于代码后台方式。
你不必映射appsettings中的所有内容,只需要映射你的代码中感兴趣的属性。反序列化会忽略其他属性,只映射配置类中提到的属性。
英文:
Make use of IConfiguration.Get<>
it is an extension method.
Let say I wanted to load a C# model called ChatSettings.cs
public class ChatSettings
{
public string BaseUrl { get; set; }
public string Scope { get; set; }
}
I update my appsettings to look like this:
{
...
"Chat": {
"BaseUrl": "https://localhost:7040/",
"Scope": "api://xxxx-xxx-xxxx-xxxx-xxxxxx/access_as_user"
}
...
}
Now create a model for the root of your appsettings.json
public class LocalConfigurations
{
public ChatSettings Chat { get; set; }
}
In program.cs
var config = builder.Configuration.Get<LocalConfigurations>();
If you wish to access this info from another service or razor component add this
builder.Services.AddSingleton(_ => config);
In a service just add it to your constructor.
public class SomeService
{
public SomeService(LocalConfigurations config)
{
ChatSettings chatConfig = config.Chat;
}
}
In a .razor file use the @inject or [Inject] attribute on the property
@inject LocalConfigurations Config
...
@code {
[Inject]
private LocalConfigurations Config { get; set; }
}
Use one method of injecting not both. I prefer the attribute way as I prefer the code behind approach.
You do not have to map everything in your appsettings just the properties you are interested in for "your" code. The deserialization will ignore the other attributes and only map the properties mentioned in your config classes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论