简体   繁体   English

为什么.NET Core选项的配置不适用于泛型类型参数?

[英]Why does the configuration of .NET Core options not work with a generic type parameter?

I have a .NET Core WebApi project. 我有一个.NET Core WebApi项目。 To read the appsettings in an easy way, I configure the options to be injected with DI. 要以简单的方式阅读appsettings,我配置要注入DI的选项。 This works fine. 这很好用。 However if I try to call Configure<>() with a generic type parameter, I get an error. 但是,如果我尝试使用泛型类型参数调用Configure<>() ,我会收到错误。

Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action' 参数2:无法从'Microsoft.Extensions.Configuration.IConfigurationSection'转换为'System.Action'

The method overloads apart from Configure<T>(Action<T> configureOptions) seem not to be available anymore. 除了Configure<T>(Action<T> configureOptions)之外的方法重载似乎不再可用。

Why does the call not work with generic type parameters? 为什么调用不适用于泛型类型参数?

Startup.cs Startup.cs

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // services.AddMvc() etc...

        services.AddOptions();

        // Works fine
        services.Configure<MyOption>(Configuration.GetSection(nameof(MyOption)));
    }

    private void AddOption<T>(IServiceCollection services)
    {
        // Wont work
        services.Configure<T>(Configuration.GetSection(nameof(T)));
        services.Configure<T>(Configuration.GetSection(""));
    }
}

MyOption.cs MyOption.cs

public class MyOption
{
    public bool MyProp { get; set; }
}

appsettings.json appsettings.json

{
    "MyOption": {
        "MyProp": true
    }
}

You need to change your function AddOption like that: 您需要更改功能AddOption ,如下所示:

private void AddOption<T>(IServiceCollection services) where T : class 
{
    services.Configure<T>(Configuration.GetSection(typeof(T).Name));
}

you forgot about where T : class in the method declaration 你忘记了方法声明where T : class的位置

And after you can use this method like there: 在你可以使用这种方法之后:

AddOption<MyOption>(services);

Apparently, nameof(T) would simply return the string "T". 显然, nameof(T)只返回字符串“T”。

You need to use typeof(T).Name instead: 你需要使用typeof(T).Name代替:

services.Configure<T>(Configuration.GetSection(typeof(T).Name));

See Documentation 文档

If you want to become a good programmer, then learn to read errors and rummage through the documentations and implementations of libraries, and not write for every reason here. 如果你想成为一名优秀的程序员,那么学会阅读错误并通过图书馆的文档和实现进行翻找,而不是在这里写出任何理由。 @igor-cova has already given you the right answer. @ igor-cova已经给你正确答案了。 The next time, be careful because writing this question took much more time and effort than you yourself would have looked into the implementation of the Configure method. 下一次,要小心,因为写这个问题需要花费更多的时间和精力,而不是你自己会考虑Configure方法的实现。 Good luck Regards, Andrew 祝你好运,安德鲁

在此输入图像描述

You need to use services.Configure as below: 您需要使用services.Configure如下:

services.Configure<MyOption>(setting =>
{  
    Configuration.GetSection("MyOption").Bind(setting);  
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM