在 .Net Core 中使用 app.config [英] Using app.config in .Net Core
问题描述
我有问题.我需要在 .Net Core(C#) 中编写一个使用 app.config 的程序,如下所示:
I have problem. I need to write a program in .Net Core(C#) which use app.config like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="custom" type="ConfigurationSample.CustomConfigurationSection, ConfigurationSample"/>
</configSections>
<connectionStrings>
<add name="sampleDatabase" connectionString="Data Source=localhostSQLExpress;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);
这是来自互联网的例子,所以我认为会起作用,但我得到了例外:
and it is example from the internet so I thought would work, but I am getting exceptions:
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 下载,但仍然无法正常工作.也许有人可以帮助我?
I downloaded via NuGet - Install-Package CoreCompat.System.Configuration -Version 4.2.3-r4 -Pre and still don't work. Maybe someone can help me?
推荐答案
您可以使用 Microsoft.Extensions.Configuration API 使用任何 .NET Core 应用程序,而不仅仅是使用 ASP.NET Core 应用程序.查看链接中提供的示例,该示例显示了如何在控制台应用程序中读取配置.
You can use Microsoft.Extensions.Configuration API with any .NET Core app, not only with ASP.NET Core app. Look into sample provided in the link, that shows how to read configs in the console app.
在大多数情况下,JSON 源(读取为 .json
文件)是最合适的配置源.
In most cases, the JSON source (read as .json
file) is the most suitable config source.
注意:当有人说配置文件应该是 appsettings.json
时,不要感到困惑.您可以使用任何适合您的文件名,文件位置可能不同 - 没有特定规则.
Note: don't be confused when someone says that config file should be
appsettings.json
. You can use any file name, that is suitable for you and file location may be different - there are no specific rules.
但是,由于现实世界很复杂,有很多不同的配置提供程序:
But, as the real world is complicated, there are a lot of different configuration providers:
- 文件格式(INI、JSON 和 XML)
- 命令行参数
- 环境变量
等等.您甚至可以使用/编写自定义提供程序.
and so on. You even could use/write a custom provider.
实际上,app.config
配置文件是一个 XML 文件.因此,您可以使用 XML 配置提供程序(github 上的源代码、nuget 链接).但请记住,它将仅用作配置源 - 您的应用程序行为方式的任何逻辑都应由您实现.配置提供程序不会更改设置"并为您的应用设置政策,而只会从文件中读取数据.
Actually, app.config
configuration file was an XML file. So you can read settings from it using XML configuration provider (source on github, nuget link). But keep in mind, it will be used only as a configuration source - any logic how your app behaves should be implemented by you. Configuration Provider will not change 'settings' and set policies for your apps, but only read data from the file.
这篇关于在 .Net Core 中使用 app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!