![](/img/trans.png)
[英]Accessing Configuration Properties for the Class Decorators in ASP.NET Core
[英]Asp.Net Core configuration in static class
我想從我的 appsettings.json 文件中讀取 url static ZA2F2ED4F8EBC2CBB4C21A29 我嘗試了類似的東西
private static string Url => ConfigurationManager.GetSection("Urls/SampleUrl").ToString();
但是每當我嘗試調用GetSection()
方法時,我都會得到 null 值。
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"cs1": "some string",
"cs2": "other string"
},
"Urls": {
"SampleUrl": "google.com"
},
"AllowedHosts": "*"
我只是想從 appsettings 中讀取一些數據。 根據文檔,我不應該以某種方式注冊我的標准 appsettings.json 文件,因為程序 class 中的Host.CreateDefaultBuilder(args)
默認為我執行此操作。
正如這里提到的,您可以將 static 屬性添加到您的Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
StaticConfig = configuration;
}
public static IConfiguration StaticConfig { get; private set; }
並在 static class 中使用:
var x = Startup.StaticConfig.GetSection("whatever");
ConfigurationManager
api 無法按照您在 ASP.NET 內核中的預期方式工作。 它不會拋出任何異常,而是在您調用它的方法時簡單地返回null
,就像您遇到的那樣。
在 ASP.NET 內核中,您有新的對象和 API 可以將配置傳遞給您的應用程序。 它們基於配置源的想法,可以使用配置構建器注冊,一旦構建,配置構建器就會為您提供配置 object。 如果您使用默認主機構建器,則會自動考慮appsettings.json
文件的配置源,因此您可以直接使用它。 完整的文檔可在此處獲得。
您缺少的另一部分是 ASP.NET 內核中的 DI 容器。 該框架具有強烈的固執己見,並指導您基於依賴注入模式進行設計。 每次您的服務需要某些東西時,他們只需通過構造函數參數請求它,而其他一些參與者(DI 容器)將負責解決依賴關系並注入請求的 object。 在 DI 容器中自動注冊的接口之一是IConfiguration
接口,它基本上是您傳遞給應用程序的配置。
這在我看來你的設計是不正確的。 從 static class 訪問應用程序配置沒有意義。 Static 類通常是 static 方法的容器,這些方法基本上是接收輸入並產生 output 的函數。 將它們視為為您實現計算的純函數,並可用作解決特定問題的助手。 舉個例子,考慮下面的 static class:
public static class StringHelpers
{
// notice that this is a pure function. This code does not know it is running inside an application having some sort of injected configuration. This is only an helper function
public static string Normalize(string str)
{
if (str is null)
{
throw new ArgumentNullException(nameof(str));
}
return str.Trim().ToUpperInvariant();
}
}
您的 static 方法完全有可能需要您的一些配置作為輸入才能工作。 在這種情況下,您應該選擇以下設計:
public interface IUrlProvider
{
string GetUrl();
}
public class ConfigurationBasedUrlProvider: IUrlProvider
{
private const string DefaultUrl = "https://foo.example.com/foo/bar";
private readonly IConfiguration _appConfiguration;
// ask the DI container to inject the configuration. Here you have the content of appsettings.json for free. Interface IConfiguration is automatically registered with the DI container
public ConfigurationBasedUrlProvider(IConfiguration configuration)
{
_appConfiguration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public string GetUrl()
{
var configuredUrl = _appConfiguration.GetSection("Urls")["SampleUrl"];
var safeUrl = string.IsNullOrWhiteSpace(configuredUrl) ? DefaultUrl : configuredUrl;
return StringHelpers.Normalize(safeUrl);
}
}
利用...
Configuration.GetSection("Urls").GetValue<string>("SampleUrl");
更新:對不起,這是基於假設,配置已經被注入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.