我们有一个.NET Framework .dll正在移植到.NET Core。目前,我们正在从ConfigurationElement和ConfigurationSection继承System.Configuration,以便在app.config中创建自定义配置部分(或者它的.NET核心等效项)
问题:
Microsoft.Extensions.Configuration。对吗?因为它生活在ASP.NET Core的github项目上,而不是.NET Core的github项目上,我们没有ASP部件。.NET Core的自定义配置节的startup.cs示例?理想情况下,我们希望从文本源(XML或JSON)直接读取POCO对象图,以获得强类型的好处。ConfigurationElement和ConfigurationSection的支持,而不再需要任何这样的移植工作?我问的原因是.NET Core2.0路线图说
作为这项工作的一部分,.NET核心从.NET框架获得了超过5,000个API,使其成为一个更广泛的平台。发布于 2017-04-20 07:24:13
我不知道app.config和System.Configuration对.NET Core的支持。可能没有,但那只是猜测。您可以在.NET Core方法中为Main应用程序设置配置:
class Program
{
static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var poco = new Poco();
configuration.Bind(poco);
Console.WriteLine(poco);
Console.ReadKey();
}
}
class Poco
{
public bool Enabled { get; set; }
public Sort Sort { get; set; }
public override string ToString()
{
return $"Enabled={Enabled}, SortOrder={Sort.Order}";
}
}
class Sort
{
public int Order { get; set; }
}appsettings.json如下所示:
{
"enabled": true,
"sort": {
"order": 2
}
}产出:
Enabled=True, SortOrder=2您需要引用Microsoft.Extensions.Configuration.Json和Microsoft.Extensions.Configuration.Binder包。
不依赖于ASP.NET Core。
Microsoft.Extensions.Configuration是相当可扩展的,它可以使用环境变量、命令行参数等不同的设置提供程序。因此,如果需要,可以为ConfigurationSection-like配置实现自定义提供程序。
基于这句话,他们不会将System.Configuration引入NetStandard 2.0。
发布于 2017-05-11 15:57:01
此外,还描述了迁移到Microsoft.Extensions.Configuration的方式(这完全是有意义的),因为它应该(至少我希望)能够在.NET Core 2上使用来自System.Configuration的相同类型。
以下是corefx中的System.Configuration类型:https://github.com/dotnet/corefx/tree/master/src/System.Configuration.ConfigurationManager
我不能告诉你,它们和full .NET的完全兼容。但至少这是给我们带来希望的东西。
因此,看起来.NET核心2将有旧的System.Configuration东西,而不是netstandard2。这可能是因为MS不想在其他平台之间共享这些类型(Xamarin)。
https://stackoverflow.com/questions/43510983
复制相似问题