Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET Core中使用appsettings.json设置进行继承_C#_Asp.net_Asp.net Core - Fatal编程技术网

C# 在.NET Core中使用appsettings.json设置进行继承

C# 在.NET Core中使用appsettings.json设置进行继承,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我正在努力实现这样的目标: services.Configure<Child1Settings>(hostContext.Configuration.GetSection("BaseSettings")); services.Configure<Child1Settings>(hostContext.Configuration.GetSection("Child1Settings")); BaseSettings-具有所有其他部分通用的设置 Child1设置-具有所有

我正在努力实现这样的目标:

services.Configure<Child1Settings>(hostContext.Configuration.GetSection("BaseSettings"));
services.Configure<Child1Settings>(hostContext.Configuration.GetSection("Child1Settings"));
  • BaseSettings-具有所有其他部分通用的设置
  • Child1设置-具有所有基本设置+Child1设置
  • Child2Settings-具有所有基本设置+Child2Settings

    [……]

  • ChildNSettings-具有所有基本设置+ChildNSettings

那么我的控制器中就有这个:

public class Child1Controller : Controller
{
    public Child1Controller(Child1Settings settings)
    {
        // settings.BaseSetting and settings.Child1Setting should both be accessible here
    }
}
我试过这个:

public class BaseSettings
{
    public string BaseSetting { get; set; }
}

public class Child1Settings : BaseSettings
{
    public string Child1Setting { get; set; }
}
appsettings.json

{
    "BaseSettings": {
        "BaseSetting": "BaseSettingValue"
    },
    "Child1Settings": {
        "Child1Setting": "Child1SettingValue"
    }
}
{
    "Child1Settings": {
        "BaseSettings": {
            "BaseSetting": "BaseSettingValue"
        }
        "Child1Setting": "Child1SettingValue"
    },
    "Child2Settings": {
        "BaseSettings": {
            "BaseSetting": "BaseSettingValue"
        }
        "Child2Setting": "Child2SettingValue"
    }
}
Startup.cs

public void ConfigureServices(IServiceCollection services)
{    
    services.AddOptions();
    services.Configure<Child1Settings>(options => Configuration.GetSection("Child1Settings").Get<Child1Settings>());
    services.AddSingleton(Configuration);
}

实现这一点的一种方法是,使用基本节数据配置子节,然后使用子节特定的数据进行配置,如下所示:

services.Configure<Child1Settings>(hostContext.Configuration.GetSection("BaseSettings"));
services.Configure<Child1Settings>(hostContext.Configuration.GetSection("Child1Settings"));
services.Configure(hostContext.Configuration.GetSection(“BaseSettings”);
services.Configure(hostContext.Configuration.GetSection(“Child1Settings”);

这样,第一次调用
Configure
将使用基本数据对其进行配置。下一次调用
Configure
将覆盖您在基本设置中配置的内容,并向其中添加额外的Child1Settings。

首先,当您删除
BaseSettings
并将
BaseSetting
置于与
Child1Setting
相同的级别时,它应该可以工作。然而,真正荒谬的事情是,为一个系统要求如此具体和很少使用的特性,这意味着允许简单的读取/序列化来支持所有很少和不可能的用例。如果没有一些复杂的问题,你怎么能期望它在一开始就起作用呢。配置甚至应该如何知道您的对象应该是“继承的”,您正在特别传递一个键
Configuration.GetSection(“Child1Settings”)
,该键只选择一个元素。任何添加任何类型继承的尝试都需要一个非常复杂的json模式,这使得在大多数情况下使用它太复杂。如果您需要一些特定的内容,您需要编写自己的解决方案,或者使用第三方扩展(如果有)从其他来源读取配置。在幕后,它只是一个简单的json反序列化,并且在分配json对象时需要对其设置所有属性。这就是JSON的工作原理,因为JSON和JavaScript(它最初来自哪里)都没有继承的概念哇+1这真的很有效!我正在寻找一种绑定兄弟节的方法。非常感谢。