当前位置:网站首页>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
边栏推荐
- An essay on the classical "tear down the wall in thinking"
- TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
- 1-4 configuration executable script of nodejs installation
- CentOS MySQL multi instance deployment
- New keyword learning and summary
- 如何用Redis实现分布式锁?
- org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
- [markdown notes]
- Net standard
猜你喜欢
ClickHouse-表引擎
Get the column name list of the table quickly in Oracle
Bytevcharts visual chart library, I have everything you want
RTKLIB 2.4.3源码笔记
C# Task. Delay and thread The difference between sleep
How vscode compares the similarities and differences between two files
Installing labellmg tutorial in Windows
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
groutine
Mock test
随机推荐
RTKLIB 2.4.3源码笔记
蓝桥杯省一之路06——第十二届省赛真题第二场
【题解】[SHOI2012] 随机树
Copy constructor shallow copy and deep copy
Sub database and sub table & shardingsphere
如何用Redis实现分布式锁?
CentOS MySQL multi instance deployment
PHP efficiently reads large files and processes data
◰GL-着色器处理程序封装
Baidu Map Case - Zoom component, map scale component
Website_ Collection
[C#] 彻底搞明白深拷贝
freeCodeCamp----prob_ Calculator exercise
Baidu Map Case - modify map style
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
[markdown notes]
Modify the test case name generated by DDT
Detailed explanation of Niuke - Gloves
Further optimize Baidu map data visualization
MySQL modify master database