- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 204字
- 2021-06-18 18:35:56
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.
- Intel Galileo Essentials
- 兩周自制腳本語言
- Python從菜鳥到高手(第2版)
- Selenium Design Patterns and Best Practices
- 碼上行動:零基礎學會Python編程(ChatGPT版)
- 人臉識別原理及算法:動態人臉識別系統研究
- Kotlin Standard Library Cookbook
- SharePoint Development with the SharePoint Framework
- Yocto for Raspberry Pi
- Java Web程序設計任務教程
- ANSYS Fluent 二次開發指南
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Learning PHP 7
- 進入IT企業必讀的324個Java面試題
- Java高并發編程詳解:深入理解并發核心庫