- 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.
- Effective C#:改善C#代碼的50個(gè)有效方法(原書第3版)
- Python自動(dòng)化運(yùn)維快速入門(第2版)
- Django Design Patterns and Best Practices
- SAS數(shù)據(jù)統(tǒng)計(jì)分析與編程實(shí)踐
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開發(fā)
- Spring+Spring MVC+MyBatis整合開發(fā)實(shí)戰(zhàn)
- C語(yǔ)言程序設(shè)計(jì)教程
- Access 2010數(shù)據(jù)庫(kù)應(yīng)用技術(shù)(第2版)
- Swift Playgrounds少兒趣編程
- Mastering C++ Multithreading
- Node學(xué)習(xí)指南(第2版)
- JavaScript程序設(shè)計(jì)(第2版)
- Webpack實(shí)戰(zhàn):入門、進(jìn)階與調(diào)優(yōu)(第2版)
- Kotlin語(yǔ)言實(shí)例精解
- 大話程序員:從入門到優(yōu)秀全攻略