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

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.

主站蜘蛛池模板: 高雄县| 昌江| 淮阳县| 凌云县| 铜陵市| 通渭县| 皮山县| 东方市| 富裕县| 杂多县| 凤冈县| 鲁山县| 乐都县| 娄底市| 四平市| 建阳市| 承德市| 共和县| 云龙县| 静海县| 娄烦县| 天镇县| 闸北区| 厦门市| 新干县| 罗源县| 尉氏县| 遂溪县| 凤凰县| 新泰市| 延津县| 沭阳县| 淳安县| 静宁县| 桃园县| 剑川县| 桃源县| 凤城市| 沿河| 阿拉尔市| 息烽县|