从 .net 标准库中读取 appsettings.json
Reading appsettings.json from .net standard library
我已经使用 .NET Core Framework 开始了一个新的 RESTful 项目。
我将我的解决方案分为两部分:框架(一组 .NET 标准库)和 Web(RESTful 项目)。
通过 Framework 文件夹,我为进一步的 Web 项目提供了一些库,并且我想在其中一个中提供配置 class 和通用方法 T GetAppSetting<T>(string Key)
.
我的问题是:我怎样才能访问 .NET Standard 中的 AppSettings.json 文件?
我找到了很多关于读取此文件的示例,但是所有这些示例都将文件读取到 Web 项目中,但没有人将其读取到外部库中。我需要它为其他项目提供可重用的代码。
正如评论中已经提到的,你真的不应该这样做。 Inject a configured IOptions<MyOptions>
using dependency injection instead.
但是,您仍然可以加载 json 文件作为配置:
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Use configuration as in every web project
var myOptions = configuration.GetSection("MyOptions").Get<MyOptions>();
确保引用 Microsoft.Extensions.Configuration
和 Microsoft.Extensions.Configuration.Json
包。更多配置选项 see the documentation.
我扩展了这个场景以便管理也(可选)用户机密(来自包:Microsoft.Extensions.Configuration.UserSecrets
):
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
.Build();
添加 Json 文件和用户机密的顺序在这里很重要。参见
我完全同意这不是首选方式(而是使用 IOptions<>
和 Dependency Injection
并让应用程序配置库)。但我之所以提到这一点,是因为我正在研究一个(非常古老的)图书馆,该图书馆正在阅读 app.config (xml)。无法从应用程序配置此库,而是库直接进行配置(期望 app.config 中的值)。该库现在用于 Full Framework、.NET Core 和 .NET5(或更新版本)应用程序。所以我也不得不支持appsettings.json。实际上不可能以某种方式调整库,以便应用程序可以向库提供必要的配置值。因此,我添加了对 JSON 的支持(暂时 - 也许以后我们可以花更多的精力让它可以从应用程序配置)
最后我也支持 environments 我的代码是这样的:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (!string.IsNullOrEmpty(environment))
{
builder = builder.AddJsonFile(string.Format("appsettings.{0}.json", environment), optional: true, reloadOnChange: false);
if (string.Equals(environment, "Development", StringComparison.CurrentCultureIgnoreCase))
{
builder = builder.AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
}
}
请注意,我决定仅针对 开发 范围管理 用户机密。
我已经使用 .NET Core Framework 开始了一个新的 RESTful 项目。
我将我的解决方案分为两部分:框架(一组 .NET 标准库)和 Web(RESTful 项目)。
通过 Framework 文件夹,我为进一步的 Web 项目提供了一些库,并且我想在其中一个中提供配置 class 和通用方法 T GetAppSetting<T>(string Key)
.
我的问题是:我怎样才能访问 .NET Standard 中的 AppSettings.json 文件?
我找到了很多关于读取此文件的示例,但是所有这些示例都将文件读取到 Web 项目中,但没有人将其读取到外部库中。我需要它为其他项目提供可重用的代码。
正如评论中已经提到的,你真的不应该这样做。 Inject a configured IOptions<MyOptions>
using dependency injection instead.
但是,您仍然可以加载 json 文件作为配置:
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Use configuration as in every web project
var myOptions = configuration.GetSection("MyOptions").Get<MyOptions>();
确保引用 Microsoft.Extensions.Configuration
和 Microsoft.Extensions.Configuration.Json
包。更多配置选项 see the documentation.
我扩展了这个场景以便管理也(可选)用户机密(来自包:Microsoft.Extensions.Configuration.UserSecrets
):
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
.Build();
添加 Json 文件和用户机密的顺序在这里很重要。参见
我完全同意这不是首选方式(而是使用 IOptions<>
和 Dependency Injection
并让应用程序配置库)。但我之所以提到这一点,是因为我正在研究一个(非常古老的)图书馆,该图书馆正在阅读 app.config (xml)。无法从应用程序配置此库,而是库直接进行配置(期望 app.config 中的值)。该库现在用于 Full Framework、.NET Core 和 .NET5(或更新版本)应用程序。所以我也不得不支持appsettings.json。实际上不可能以某种方式调整库,以便应用程序可以向库提供必要的配置值。因此,我添加了对 JSON 的支持(暂时 - 也许以后我们可以花更多的精力让它可以从应用程序配置)
最后我也支持 environments 我的代码是这样的:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (!string.IsNullOrEmpty(environment))
{
builder = builder.AddJsonFile(string.Format("appsettings.{0}.json", environment), optional: true, reloadOnChange: false);
if (string.Equals(environment, "Development", StringComparison.CurrentCultureIgnoreCase))
{
builder = builder.AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
}
}
请注意,我决定仅针对 开发 范围管理 用户机密。