官术网_书友最值得收藏!

Configuring the runtime host

.NET Core 3 introduced a not-so-well-known configuration mechanism that still has some use: a runtime host configuration. The idea here is that you provide configuration settings, as key-value pairs, in the .csproj file. You can retrieve them programmatically from the AppContext class. Here is an example project file:

<ProjectSdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<RuntimeHostConfigurationOptionInclude="Foo"Value="Bar"/>
</ItemGroup>
</Project>

The "Foo" setting is retrievable through a call to the GetData method of the AppContext class, as illustrated in the following code snippet:

var bar = AppContext.GetData("Foo");

If the named entry does not exist, GetData just returns null. Mind you, GetData is prototyped as returning an object, but in this case, it will return a string.

Normally, you wouldn't want to do that, but should you ever want to create or modify one entry of a runtime host configuration setting, you can do that through the application domain, as follows:

AppDomain.CurrentDomain.SetData("Foo", "ReBar");

Mind you, this is not a replacement for a well-structured and properly defined configuration. What .NET Core does is, at run and deployment time, it copies the contents of the RuntimeHostConfigurationOption sections (and some more) to a generated ${project}.runtimeconfig.json file that is placed together with the generated binary.

We'll now see a new feature of ASP.NET Core: feature toggles.

主站蜘蛛池模板: 泽库县| 南宫市| 宁河县| 平江县| 堆龙德庆县| 永泰县| 竹山县| 长乐市| 桦甸市| 昌图县| 惠东县| 新平| 馆陶县| 盐边县| 山丹县| 盐城市| 新泰市| 延边| 菏泽市| 枞阳县| 宣化县| 青海省| 娱乐| 吴旗县| 云南省| 安陆市| 瑞昌市| 牙克石市| 藁城市| 安远县| 鞍山市| 平昌县| 新建县| 根河市| 衡山县| 招远市| 会同县| 宜昌市| 天津市| 额济纳旗| 新乡县|