Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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#ASP.Net 5配置和与类库的向后兼容性_C#_Asp.net - Fatal编程技术网

C#ASP.Net 5配置和与类库的向后兼容性

C#ASP.Net 5配置和与类库的向后兼容性,c#,asp.net,C#,Asp.net,我有多个遗留库,它们通过ConfigurationManager进行自我配置。例如: var port = ConfigurationManager.AppSettings["service.port"]; 新的ASP.Net系统更喜欢基于附加的“config.json”文件的强类型模型。示例(摘自): //来自AppSettings.cs 公共类应用程序设置 { 公共字符串SiteTitle{get;set;} } //从config.json { “应用设置”:{ “SiteTitle”:

我有多个遗留库,它们通过ConfigurationManager进行自我配置。例如:

var port = ConfigurationManager.AppSettings["service.port"];
新的ASP.Net系统更喜欢基于附加的“config.json”文件的强类型模型。示例(摘自):

//来自AppSettings.cs
公共类应用程序设置
{
公共字符串SiteTitle{get;set;}
}
//从config.json
{
“应用设置”:{
“SiteTitle”:“WebApplication2”,
},
“数据”:{
“默认连接”:{
“ConnectionString”:“服务器=(localdb)\\mssqllocaldb;数据库=blahfoo;受信任的连接=True;MultipleActiveResultSets=True”
}
}
}
//从Startup.cs
公营创业
{
公共IConfiguration配置{get;set;}
公共启动(IHostingEnvironment环境)
{
//设置配置源。
var配置=新配置()
.AddJsonFile(“config.json”)
.AddJsonFile($“config.{env.EnvironmentName}.json”,可选:true);
configuration.AddEnvironmentVariables();
配置=配置;
}
public void配置服务(IServiceCollection服务)
{
//将应用程序设置添加到服务容器。
services.Configure(Configuration.GetSubKey(“AppSettings”);
…
}
}
我的问题是:有没有一种方法可以接受新的ASP.NET5及其强类型配置方法,同时保持与其他应用程序库的向后兼容性


或者更确切地说,我可以使用我们的产品组合中的公共库而不必重写它们吗?

您的问题是,您依赖于具体的配置实现,并使用类中的
ConfigurationManager
的静态成员,而不是编写具有适当依赖注入的可靠实现

您可能会发现一些不需要更改代码就可以使用新配置模型的黑客技巧,但我认为您应该帮自己一个忙,并利用这一机会实际重新考虑您的代码,并将当前配置抽象到一个简单的界面后面,例如:


公共接口IMyAppConfiguration
{
字符串设置1{get;}
字符串设置2{get;}
SomeOtherMoreComplexSetting设置3{get;}
}

然后在需要设置的每个类中注入此依赖项,并提供一个封装当前ConfigurationManager类的实现和另一个封装新配置模型的实现


这是一个很好的例子,说明了为什么坚固的设计很重要,如果做得好,代码维护和创新会更容易。

以前的ASP.NET版本也有强类型配置类,否则您将无法拥有配置部分和特定元素。不过,我认为您不能使用旧的配置类来加载json类。您可以创建公共库,但这需要以我理解的方式重写弱类型的配置代码。问题是我们有很多表面积和很多遗留代码。虽然我完全同意应该彻底检修遗留库,但在这一点上是不可行的。我需要的是一种折衷方案,它允许我推进新技术(强制要求配置类),而无需维护相同库的多个副本或重写整个系统。检查,它允许您轻松设计配置部分和元素,并生成所需的样板代码(有很多)但是新模型要简单得多,因此可能需要做一些工作才能将旧系统中的类转换为新DTO。如果结构匹配听起来像是需要在旧系统中使用新类,那么AutoMapper可能会有所帮助,因此您不必重写旧代码。这可能更容易,配置系统是可插入的。您可以签署一个从数据库或Json文件读取的新配置提供程序。至少有一个示例使用该数据库找到了至少一个示例,以及一个相关的
//from AppSettings.cs
public class AppSettings
{
    public string SiteTitle { get; set; }
}

//from config.json
{
    "AppSettings": {
        "SiteTitle": "WebApplication2",
    },
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=blahfoo;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    }
}

// from Startup.cs
public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        // Setup configuration sources.
        var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

        configuration.AddEnvironmentVariables();
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Application settings to the services container.
        services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

        …
    }
}