当前位置:网站首页>ASP. Net core configuration options (Part 2)
ASP. Net core configuration options (Part 2)
2022-04-23 17:06:00 【begeneral】
The configuration items used in this article correspond to POCO object :
public class Profile
{
public Gender Gender { get; set; }
public int Age { get; set; }
public ContactInfo ContactInfo { get; set; }
}
public class ContactInfo
{
public string EmailAddress { get; set; }
public string PhoneNo { get; set; }
}
1、IOptionsSnapshot
In the last article we introduced IOptions and IOptionsMonitor, Here we talk about IOptionsSnapshot. Let's take a look at the following configuration file :
{
"foo": {
"gender": "Male",
"age": "18",
"contactinfo": {
"emailAddress": "foobar.outlook.com",
"phoneno": "123"
}
},
"bar": {
"gender": "Female",
"age": "20",
"contactinfo": {
"emailAddress": "foobar.outlook.com",
"phoneno": "456"
}
}
}
Here you are 2 Configurations , The fields of configuration items are the same , But each configuration has its own name :foo and bar. It's like a class with many students , Every student has his own attributes ( full name 、 Age etc. ).
The use of this configuration is not much different from the two introduced in the previous article , Let's take a look at the code :
This is startup Of ConfigureServices Code in function :
var configuration1 = new ConfigurationBuilder().AddJsonFile(path: "profile1.json", optional: false, reloadOnChange: true).Build();
services.AddOptions().Configure<Profile>("foo", configuration1.GetSection("foo"));
services.AddOptions().Configure<Profile>("bar", configuration1.GetSection("bar"));
This is the code to get the configuration :
var fooProfile = _snapOptions.Get("foo");
var barProfile = _snapOptions.Get("bar");
_snapOptions Is a defined global variable :IOptionsSnapshot<Profile> _snapOptions, Assign values to it through dependency injection .
This method actually gives the configuration a name , Then get the configuration by name .
2、MemoryConfigurationSource
Previously, we used configuration files , Because this is the most common configuration , Let's take a look at using memory to configure .
var memorySource = new Dictionary<string, string>
{
["gender"] = "Male",
["age"] = "20",
["ContactInfo:emailaddress"] = "[email protected]",
["ContactInfo:phoneno"] = "123"
};
var memeoryConfig = new ConfigurationBuilder().AddInMemoryCollection(memorySource).Build();
services.AddOptions().Configure<Profile>(memeoryConfig);
gender and age yes Profile Attributes of a class , So you can assign a value directly . But the last two are ContactInfo Attributes of a class , So we need to ContactInfo Put it in front , Then use a colon to refer to the properties of this class . Note here that it must be a colon , Not commas .
The way to get the configuration is the same as before , I won't go into that here .
版权声明
本文为[begeneral]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554081823.html
边栏推荐
- Kingdee Cloud Star API calling practice
- VsCode-Go
- Mock test
- How to implement distributed locks with redis?
- Getting started with JDBC
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
- Shortcut keys (multiline)
- ASP. Net core reads the configuration file in the class library project
- Freecodecamp ---- budget & category exercise
- Generate random numbers with high quality and Gaussian distribution
猜你喜欢

. net cross platform principle (Part I)

How vscode compares the similarities and differences between two files

ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation

Milvus 2.0 質量保障系統詳解

【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明

Detailed explanation of the penetration of network security in the shooting range

Zhongang Mining: Fluorite Flotation Process

Sub database and sub table & shardingsphere

Milvus 2.0 质量保障系统详解

Change the password after installing MySQL in Linux
随机推荐
Wiper component encapsulation
El cascade and El select click elsewhere to make the drop-down box disappear
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
Zhongang Mining: Fluorite Flotation Process
. net type transfer
_ Mold_ Board_
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
SQL database
Feign report 400 processing
Quick install mongodb
Detailed explanation of Niuke - Gloves
Nacos + aspnetcore + Ocelot actual combat code
El date picker limits the selection range from the current time to two months ago
oracle 中快速获取表的列名列表
PHP efficiently reads large files and processes data
JSON deserialize anonymous array / object
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Generate random numbers with high quality and Gaussian distribution
1-2 characteristics of nodejs