我有问题。我需要在 .Net Core(C#) 中编写一个使用 app.config 的程序,例如:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="custom" type="ConfigurationSample.CustomConfigurationSection, ConfigurationSample"/>
</configSections>
<connectionStrings>
<add name="sampleDatabase" connectionString="Data Source=localhost\SQLExpress;Initial Catalog=SampleDatabase;Integrated Security=True"/>
</connectionStrings>
<appSettings>
<add key="sampleApplication" value="Configuration Sample"/>
</appSettings>
<custom>
<customConfigurations>
<add key="customSample" name="Mickey Mouse" age="83"/>
</customConfigurations>
</custom>
</configuration>
而我写道:
string connectionString = ConfigurationManager.ConnectionStrings["sampleDatabase"].ConnectionString;
Console.WriteLine(connectionString);
// read appSettings configuration
string appSettingValue = ConfigurationManager.AppSettings["sampleApplication"];
Console.WriteLine(appSettingValue);
这个例子是从互联网上找到的,我以为能够正常运行,但是我遇到了异常:
System.Configuration.ConfigurationErrorsException: 'Error Initializing the configuration system.'
Inner Exception
TypeLoadException: Could not load type 'System.Configuration.InternalConfigurationHost' from assembly 'CoreCompat.System.Configuration, Version=4.2.3.0, Culture=neutral, PublicKeyToken=null' because the method 'get_bundled_machine_config' has no implementation (no RVA).
我通过NuGet下载了 - Install-Package CoreCompat.System.Configuration -Version 4.2.3-r4 -Pre,但仍然无法运行。也许有人可以帮助我吗?