日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET 6 中的 ConfigurationManager

發(fā)布時間:2023/12/4 asp.net 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET 6 中的 ConfigurationManager 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

.NET 6 中的 ConfigurationManager

Intro

.NET 6 為了 Minimal API 引入了一些新東西,其中就包含了一個全新的配置對象 ConfigurationManager

這并不是 .NET Framework 里的靜態(tài)類?ConfigurationManager,而是 .NET Core 里的配置 Microsoft.Extensions.Configuration 中的一個新類型,新的 ConfigurationManager 對象是之前的 ConfigurationBuilder 和 ConfigurationRoot 的結合

Implement

API Proposal:

namespace?Microsoft.Extensions.Configuration { +????public?sealed?class?ConfigurationManager?:?IConfigurationRoot,?IConfigurationBuilder,?IDisposable +????{ +????????public?ConfigurationManager(); +????????public?string??this[string?key]?{?get;?set;?} +????????public?IConfigurationSection?GetSection(string?key); +????????public?void?Dispose(); +????}

Implement:

public?sealed?class?ConfigurationManager?:?IConfigurationBuilder,?IConfigurationRoot,?IDisposable {private?readonly?ConfigurationSources?_sources;private?readonly?ConfigurationBuilderProperties?_properties;private?readonly?object?_providerLock?=?new();private?readonly?List<IConfigurationProvider>?_providers?=?new();private?readonly?List<IDisposable>?_changeTokenRegistrations?=?new();private?ConfigurationReloadToken?_changeToken?=?new();///?<summary>///?Creates?an?empty?mutable?configuration?object?that?is?both?an?<see?cref="IConfigurationBuilder"/>?and?an?<see?cref="IConfigurationRoot"/>.///?</summary>public?ConfigurationManager(){_sources?=?new?ConfigurationSources(this);_properties?=?new?ConfigurationBuilderProperties(this);//?Make?sure?there's?some?default?storage?since?there?are?no?default?providers.this.AddInMemoryCollection();AddSource(_sources[0]);}//?... }

ConfigurationManager在添加 ConfigurationSource 的時候也會注冊 IConfigurationProvider,這樣在添加 Source 之后就能夠拿到 Configuration 中的配置了,在實現(xiàn)上,微軟封裝了一個私有的 ConfigurationSource 的類型,這里我們看一下注冊配置源的代碼

private?class?ConfigurationSources?:?IList<IConfigurationSource> {private?readonly?List<IConfigurationSource>?_sources?=?new();private?readonly?ConfigurationManager?_config;public?ConfigurationSources(ConfigurationManager?config){_config?=?config;}public?IConfigurationSource?this[int?index]{get?=>?_sources[index];set{_sources[index]?=?value;_config.ReloadSources();}}public?void?Add(IConfigurationSource?source){_sources.Add(source);_config.AddSource(source);}//?... }

ConfigurationManager 中的 AddSource 方法實現(xiàn)如下:

IConfigurationBuilder?IConfigurationBuilder.Add(IConfigurationSource?source) {_sources.Add(source????throw?new?ArgumentNullException(nameof(source)));return?this; }private?void?RaiseChanged() {var?previousToken?=?Interlocked.Exchange(ref?_changeToken,?new?ConfigurationReloadToken());previousToken.OnReload(); }//?Don't?rebuild?and?reload?all?providers?in?the?common?case?when?a?source?is?simply?added?to?the?IList. private?void?AddSource(IConfigurationSource?source) {lock?(_providerLock){var?provider?=?source.Build(this);_providers.Add(provider);provider.Load();_changeTokenRegistrations.Add(ChangeToken.OnChange(()?=>?provider.GetReloadToken(),?()?=>?RaiseChanged()));}RaiseChanged(); }

可以看到每次新加一個配置源的時候,都會去構建對應的一個 IConfigurationProvider 而且會去加載配置數(shù)據(jù)并注冊配置更新事件,所以我們注冊完配置之后才能夠獲取到配置,更多實現(xiàn)細節(jié)參考 Github 上的源碼:https://github.com/dotnet/runtime/blob/v6.0.0-rc.1.21451.13/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs

Sample

來看下面使用時的 Sample 吧,非常的簡單

const?string?testKey?=?"test";var?configuration?=?new?ConfigurationManager(); Console.WriteLine(configuration[testKey]);configuration.AddInMemoryCollection(new?Dictionary<string,?string>() {{?testKey,?"test"?} }); Console.WriteLine(configuration[testKey]); Console.ReadLine();

輸出結果如下:

第一次輸出的時候還沒有注冊配置輸出的是空,第一次輸出的時候已經(jīng)注冊了配置輸出的是我們配置的值

代碼示例在可以從 Github 獲取 https://github.com/WeihanLi/SamplesInPractice/blob/master/net6sample/ConfigurationManagerSample/Program.cs

More

目前來說,ConfigurationManager 對象主要是為了 .NET 6 的 Minimal API 的需要,.NET 6 的 Minimal API 里用了這個,可以參考:https://github.com/dotnet/aspnetcore/blob/v6.0.0-rc.1.21452.15/src/DefaultBuilder/src/WebApplicationBuilder.cs ,但就像上面的示例一樣,我們也是可以直接使用的,而且原來的 IConfigurationBuilder 依然是可以用的,無需擔心升級到 .NET 6 會 break 的問題。

對于需要用到配置的測試程序直接用 ConfigurationManager 會更為簡單一些,不需要先聲明一個 ConfigurationBuilder 的對象注冊好配置之后再構建一個 IConfiguration 對象,直接用一個對象就可以了,至少從我們寫代碼的角度會簡單很多,但是性能會稍差一些,注冊的配置源越多越明顯,因為 ConfigurationManager 每次注冊配置源的時候都會去構建和注冊 IConfigurationProvider ?而 IConfigurationBuilder 則是在最后 Build 的時候才構建一次,不過通常我們的配置也只是啟動時只用配置一次,個人認為是可以接受的

References

  • https://github.com/dotnet/runtime/blob/v6.0.0-rc.1.21451.13/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs

  • https://github.com/dotnet/runtime/blob/v6.0.0-rc.1.21451.13/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationRoot.cs

  • https://github.com/dotnet/runtime/blob/v6.0.0-rc.1.21451.13/src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationBuilder.cs

  • https://github.com/dotnet/runtime/pull/55338

  • https://github.com/dotnet/runtime/issues/51770

  • https://github.com/dotnet/aspnetcore/blob/v6.0.0-rc.1.21452.15/src/DefaultBuilder/src/BootstrapHostBuilder.cs

  • https://github.com/dotnet/aspnetcore/blob/v6.0.0-rc.1.21452.15/src/DefaultBuilder/src/WebApplicationBuilder.cs

  • https://github.com/WeihanLi/SamplesInPractice/blob/master/net6sample/ConfigurationManagerSample/Program.cs

總結

以上是生活随笔為你收集整理的.NET 6 中的 ConfigurationManager的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。