在.Net Core中使用app.config [英] Using app.config in .Net Core

查看:837
本文介绍了在.Net Core中使用app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题。我需要在.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=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);

这是互联网上的示例,因此我认为可以,但是我遇到了例外情况:

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?

推荐答案


  1. 您可以使用 Microsoft.Extensions.Configuration API 任何 .NET Core应用程序配合使用,不仅与ASP配合使用。 NET Core应用程序。
    查看链接中提供的示例,该示例显示了如何在控制台应用程序中读取配置。

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆