激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - .NET Core開發(fā)之配置詳解

.NET Core開發(fā)之配置詳解

2022-02-27 15:55Ken.W ASP.NET教程

這篇文章給大家分享了.NET Core開發(fā)中相關(guān)配置的知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。

熟悉ASP.NET的開發(fā)者一定對(duì)web.config文件不陌生。在ASP.NET環(huán)境中,要想添加配置參數(shù),一般也都會(huì)在此文件中操作。其中最常用的莫過于AppSettings與ConnectionStrings兩項(xiàng)。而要在代碼中獲得文件中的配置信息,ConfigurationManager則是必不可少需要引入的程序集。

然而到了ASP.NET Core時(shí)代,存儲(chǔ)與讀取配置的方式都發(fā)生了改變。

如果對(duì)ASP.NET Core項(xiàng)目有所了解的話,應(yīng)該會(huì)看到過appsettings.json這個(gè)文件。這里就從JSON文件配置方式開始解釋ASP.NET Core中是如何讀取配置信息的。

假設(shè)有預(yù)先設(shè)置的appsettings.json文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
 "option1": "value1_from_json",
 "option2": 2,
 
 "subsection": {
  "suboption1": "subvalue1_from_json"
 },
 "wizards": [
  {
   "Name": "Gandalf",
   "Age": "1000"
  },
  {
   "Name": "Harry",
   "Age": "17"
  }
 ]
}

在代碼中讀取可以按下面的方式操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Program
{
  public static IConfiguration Configuration { get; set; }
 
  public static void Main(string[] args = null)
  {
    var builder = new ConfigurationBuilder()
      .SetBasePath(Directory.GetCurrentDirectory())
      .AddJsonFile("appsettings.json");
 
    Configuration = builder.Build();
 
    Console.WriteLine($"option1 = {Configuration["Option1"]}");
    Console.WriteLine($"option2 = {Configuration["option2"]}");
    Console.WriteLine(
      $"suboption1 = {Configuration["subsection:suboption1"]}");
    Console.WriteLine();
 
    Console.WriteLine("Wizards:");
    Console.Write($"{Configuration["wizards:0:Name"]}, ");
    Console.WriteLine($"age {Configuration["wizards:0:Age"]}");
    Console.Write($"{Configuration["wizards:1:Name"]}, ");
    Console.WriteLine($"age {Configuration["wizards:1:Age"]}");
    Console.WriteLine();
 
    Console.WriteLine("Press a key...");
    Console.ReadKey();
  }
}

首先,實(shí)例化一個(gè)ConfigurationBuilder對(duì)象,接著設(shè)置基礎(chǔ)路徑。

SetBasePath的操作其實(shí)是在ConfigurationBuilder的屬性字典里設(shè)置FileProvider的值。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static IConfigurationBuilder SetBasePath(this IConfigurationBuilder builder, string basePath)
{
  ...
  
  return builder.SetFileProvider(new PhysicalFileProvider(basePath));
}
 
public static IConfigurationBuilder SetFileProvider(this IConfigurationBuilder builder, IFileProvider fileProvider)
{
  ...
 
  builder.Properties[FileProviderKey] = fileProvider ?? throw new ArgumentNullException(nameof(fileProvider));
  return builder;
}

然后是添加JSON文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)
{
  ...
 
  return builder.AddJsonFile(s =>
  {
    s.FileProvider = provider;
    s.Path = path;
    s.Optional = optional;
    s.ReloadOnChange = reloadOnChange;
    s.ResolveFileProvider();
  });
}
 
public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, Action<JsonConfigurationSource> configureSource)
  => builder.Add(configureSource);

ConfigurationBuilder里添加了一個(gè)JsonConfigurationSource對(duì)象。

最后,執(zhí)行ConfigurationBuilder的Build方法,就可以得到保存配置信息的Configuration對(duì)象。

總結(jié)例子中的代碼,獲取配置信息的操作其實(shí)就分為兩步:

  1. 生成Configuration對(duì)象
  2. 按鍵值從Configuration對(duì)象中獲取信息

生成Configuration對(duì)象的步驟至少要有三個(gè)基礎(chǔ)環(huán)節(jié)。

  • 生成ConfigurationBuilder對(duì)象
  • 添加ConfigurationSource對(duì)象

創(chuàng)建Configuration對(duì)象

查看創(chuàng)建Configuration對(duì)象的代碼,會(huì)發(fā)現(xiàn)內(nèi)部利用的其實(shí)是ConfigurationSource中創(chuàng)建的ConfigurationProvider對(duì)象。

?
1
2
3
4
5
6
7
8
9
10
public IConfigurationRoot Build()
{
  var providers = new List<IConfigurationProvider>();
  foreach (var source in Sources)
  {
    var provider = source.Build(this);
    providers.Add(provider);
  }
  return new ConfigurationRoot(providers);
}

再看IConfiguratonSource接口,也只有一個(gè)Build方法。

?
1
2
3
4
public interface IConfigurationSource
{
  IConfigurationProvider Build(IConfigurationBuilder builder);
}

最終創(chuàng)建的Configuration對(duì)象,即ConfigurationRoot中包含了所有的ConfigurationProvider,說明配置信息都由這些ConfigurationProvider所提供。

跟蹤至ConfigurationRoot類型的構(gòu)造方法,果然在其生成對(duì)象時(shí),對(duì)所有ConfigurationProvider進(jìn)行了加載操作。

?
1
2
3
4
5
6
7
8
9
10
11
public ConfigurationRoot(IList<IConfigurationProvider> providers)
{
  ...
 
  _providers = providers;
  foreach (var p in providers)
  {
    p.Load();
    ChangeToken.OnChange(() => p.GetReloadToken(), () => RaiseChanged());
  }
}

比如JsonConfigurationProvider中:

?
1
2
3
4
5
6
7
8
public override void Load(Stream stream)
{
  try
  {
    Data = JsonConfigurationFileParser.Parse(stream);
  }
  ...
}

通過JSON解析器,將JSON文件的配置信息讀取至ConfigurationProvider的Data屬性中。這個(gè)屬性即是用于保存所有配置信息。

?
1
2
3
4
/// <summary>
/// The configuration key value pairs for this provider.
/// </summary>
protected IDictionary<string, string> Data { get; set; }

有了ConfigurationRoot對(duì)象后,獲取配置信息的操作就很簡(jiǎn)單了。遍歷各個(gè)ConfigurationProvider,從中獲取第一個(gè)匹配鍵值的數(shù)據(jù)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public string this[string key]
{
  get
  {
    foreach (var provider in _providers.Reverse())
    {
      string value;
 
      if (provider.TryGet(key, out value))
      {
        return value;
      }
    }
 
    return null;
  }
 
  ...
}

ConfigurationProvider對(duì)象從Data屬性獲取配置的值。

?
1
2
public virtual bool TryGet(string key, out string value)
  => Data.TryGetValue(key, out value);

在最初的例子中可以看Configuration["wizards:0:Name"]這樣的寫法,這是因?yàn)樵贚oad文件時(shí),存儲(chǔ)的方式就是用:為分隔符,以作為嵌套對(duì)象的鍵值。

也可以用另一種方法來寫,將配置信息綁定為對(duì)象。

先定義對(duì)象類型:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class AppSettings
{
  public string Option1 { get; set; }
  public int Option2 { get; set; }
  public Subsection Subsection { get; set; }
  public IList<Wizards> Wizards { get; set; }
}
 
public class Subsection
{
  public string Suboption1 { get; set; }
}
 
public class Wizards
{
  public string Name { get; set; }
  public string Age { get; set; }
}

再綁定對(duì)象:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static void Main(string[] args)
{
  var builder = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json");
 
  Configuration = builder.Build();
 
  var appConfig = new AppSettings();
  Configuration.Bind(appConfig);
 
  Console.WriteLine($"option1 = {appConfig.Option1}");
  Console.WriteLine($"option2 = {appConfig.Option2}");
  Console.WriteLine(
    $"suboption1 = {appConfig.Subsection.Suboption1}");
  Console.WriteLine();
 
  Console.WriteLine("Wizards:");
  Console.Write($"{appConfig.Wizards[0].Name}, ");
  Console.WriteLine($"age {appConfig.Wizards[0].Age}");
  Console.Write($"{appConfig.Wizards[1].Name}, ");
  Console.WriteLine($"age {appConfig.Wizards[1].Age}");
  Console.WriteLine();
 
  Console.WriteLine("Press a key...");
  Console.ReadKey();
}

寫法變成了常見的對(duì)象調(diào)用屬性方式,但結(jié)果是一樣的。

除了可以用JSON文件存儲(chǔ)配置信息外,ASP.NET Core同時(shí)也支持INI與XML文件。當(dāng)然有其它類型文件時(shí),也可以通過實(shí)現(xiàn)IConfigurationSource接口并繼承ConfigurationProvider類建立自定義的ConfigrationProvider對(duì)象來加載配置文件。

至于文件以外的方式,ASP.NET Core也提供了不少。

  • 命令行,AddCommandLine
  • 環(huán)境變量,AddEnvironmentVariables
  • 內(nèi)存, AddInMemoryCollection
  • 用戶機(jī)密,AddUserSecrets
  • Azure Key Vault,AddAzureKeyVault

選擇何種存儲(chǔ)與讀取配置的方法取決于實(shí)際場(chǎng)景,ASP.NET Core已經(jīng)開放了配置方面的入口,任何接入方式理論上都是可行的。實(shí)踐方面,則需要開發(fā)者們不斷去嘗試與探索。

原文鏈接:https://www.cnblogs.com/kenwoo/p/9424398.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性欧美xxxx免费岛国不卡电影 | 毛片大全在线观看 | 成人激情综合网 | 精品国产一区二区三区四区阿崩 | 天天黄色片 | 护士xxxx| 国产91精品一区二区麻豆亚洲 | 深夜小视频在线观看 | 精品国产一区二区三区天美传媒 | 久久成人午夜视频 | 一级性色 | 99久久久精品免费观看国产 | 在线a毛片 | 日韩黄色av网站 | 毛片在哪看 | 日韩三区视频 | 91成人久久 | 一级片九九 | 日本一级黄色大片 | www久久艹 | 久久99国产精品视频 | 久久国产成人午夜av浪潮 | 欧美一级网 | 一级电影免费看 | 一夜新娘第三季免费观看 | 久久精品日产高清版的功能介绍 | 色屁屁xxxxⅹ在线视频 | va视频在线 | 色97在线| 九九精品在线观看视频 | 91网页视频入口在线观看 | 少妇一级淫片免费放正片 | 91美女视频在线 | 精品国产看高清国产毛片 | 欧美人成在线 | 日韩精品久久久久久久电影99爱 | 国产成人免费高清激情视频 | 2019天天干夜夜操 | 日韩视频一二三 | 在线亚洲综合 | 国产一区二区三区高清 |