1. 程式人生 > >ASP.NET Core 2.2 基礎知識(六) 配置(內含MySql+EF)

ASP.NET Core 2.2 基礎知識(六) 配置(內含MySql+EF)

先上一段程式碼,瞭解一下 .NET Core 配置資料的結構.

新建一個 控制檯專案,新增一個檔案 json.json ,檔案內容如下:

{
  "country": "cn",
  "person": {
    "id": 1,
    "address": {
      "addName": "chengdu"
    }
  }
}

 

控制檯程式碼:

        private static void Main(string[] args)
        {
            ConfigurationBuilder builder 
= new ConfigurationBuilder(); builder.AddJsonFile(path: @"E:\xxx\my\core\VS2017\MyConfig\Demo2\json.json", optional: false, reloadOnChange: true); IConfigurationRoot config = builder.Build(); Console.WriteLine(config["country"]);//cn Console.WriteLine(config["
person:id"]);//1 Console.WriteLine(config["person:address:addname"]);//chengdu Console.ReadKey();//修改 json.json 檔案中 "id":2 Console.WriteLine(config["person:id"]);//2 Console.ReadKey(); }

 

AddJsonFile 方法有多個過載,上面只給出了其中一個,3個引數分別表示:
path:檔案的物理路徑;
optional: xml 文件是這樣寫的:Whether the file is optional. 該檔案是否可選.true 表示可選,即如果該檔案不存在,不會丟擲異常,下面的所有顯示都為空;false 表示不可選,即如果該檔案不存在,會丟擲異常.
reloadOnChange:如果檔案修改了,是否重新載入.

 

配置提供程式

ASP.NET Core 常用的共有6種配置提供程式 : 

  • 命令列配置提供程式
  • 環境變數配置提供程式
  • 檔案配置提供程式
  • Key-per-file配置提供程式
  • 記憶體配置提供程式
  • 自定義配置提供程式

一.命令列配置提供程式 : AddCommandLine

 

1.新建一個 WebAPI 專案,新建一個 TestController

    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        private readonly IConfiguration _config;

        public TestController(IConfiguration configuration)
        {
            _config = configuration;
        }

        public string Get()
        {
            //讀取配置資料中 Key 為 CommandLineKey 的值,如果沒有這個 Key,則返回預設值: defaultValue
       //讀取配置檔案的方法後面會單獨講.
return _config.GetValue("CommandLineKey","defaultValue"); } }

2.修改 CreateWebHostBuilder 方法

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var config = new ConfigurationBuilder().AddCommandLine(args).Build();
            var hostBuilder = WebHost.CreateDefaultBuilder(args);
            return hostBuilder.UseConfiguration(config).UseStartup<Startup>();
        }
    }

3.測試:

1)不傳引數

2)傳入引數

 

 

 

傳參的格式有多種:

dotnet run CommandLineKey1=Value1

dotnet run --CommandLineKey2=Value2

dotnet run --CommandLineKey3 Value3

dotnet run /CommandLineKey4=Value4

dotnet run /CommandLineKey5 Value5

此外,傳入的以單橫槓"-"或者雙橫槓"--"作為字首的 Key 支援替換:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {        
            Dictionary<string, string> switchMapping = new Dictionary<string, string>
            {
                {"-key", "CommandLineKey"},
                {"--key", "CommandLineKey"},
            };

            IConfigurationRoot config = new ConfigurationBuilder().AddCommandLine(args, switchMapping).Build();
            return WebHost.CreateDefaultBuilder().UseConfiguration(config).UseStartup<Startup>();
        }
    }

測試圖:

兩種情況都會顯示

 實際上,在 2.X 版本,CreateDefaultBuilder(args) 方法內部已經呼叫了 AddCommandLine(args) 方法,我們不需要再調一次了.原始碼(部分)如下:

 

二.環境變數配置提供程式 : AddEnvironmentVariables 

在 2.X 版本,CreateDefaultBuilder(args) 方法內部已經呼叫了 AddEnvironmentVariables() 方法,我們不需要再調一次了.原始碼(部分)如下:

 

其實,紅框框起來的方法都是配置提供程式.

那麼,環境變數到底有哪些呢?哪裡可以看呢?

新建如下控制器:

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        private readonly IConfiguration _config;

        public TestController(IConfiguration configuration)
        {
            _config = configuration;
        }

        public IEnumerable<KeyValuePair<string, string>> GetAll()
        {
            return _config.AsEnumerable();
        }

        public string Get(string key)
        {           
            return _config.GetValue(key, "defaultValue");
        }
    }

 

太多了,只截了其中一小部分:

 

我們試試查詢紅框標註的環境變數:

 

三.檔案配置提供程式 : AddJsonFile , AddIniFile , AddXmlFile

以 AddJsonFile 為例講解,其他兩個都差不多.

從上面的原始碼截圖中我們已經看到,在使用 CreateDefaultBuilder 方法初始化新的 WebHostBuilder 時,會自動呼叫 AddJsonFile 兩次,依次從下面兩個檔案載入配置:

appsettings.json : 首先讀取該檔案.

appsettings.{Environment}.json : 再讀取此檔案,該檔案中的配置會替代 appsettings.json 檔案中的值.

示例: (紅色部分是自己加的)

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "FirstSection": {
    "SecondSection": "hello world" 
  } 
}

appsettings.{Environment}.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "FirstSection": {
    "SecondSection": "fuck u"
  } 
}

請求結果:

 

下面我們來建立自己的配置檔案:

在當前專案下新建一個 jsonconfig.json 檔案:

{
  "id": 1,
  "name": "wjire" 
}

CreateWebHostBuilder 方法修改如下:

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            IWebHostBuilder hostBuilder = WebHost.CreateDefaultBuilder(args);
            return hostBuilder.ConfigureAppConfiguration((context, builder) =>
                {
                    builder.AddJsonFile(Path.Combine(context.HostingEnvironment.ContentRootPath, "jsonconfig.json"),
                        true, true);
                }).UseStartup<Startup>();
        }

 圖就不上了...

 

附:

AddIniFile 配置檔案:

[section0]
key0=value
key1=value
[section1]
subsection:key=value
[section2:subsection0]
key=value
[section2:subsection1]
key=value

 

AddXmlFile 配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<section0>
<key0>value</key0>
<key1>value</key1>
</section0>
<section1>
<key0>value</key0>
<key1>value</key1>
</section1>
</configuration>

 

四.Key-per-file 配置提供程式  AddKeyPerFile

這個提供程式有點特別,它是將檔名(含副檔名)作為 Key,檔案內容作為 Value.

示例:

在當前專案下新建一個 filename.txt 檔案,檔案內容就一句話: hello world 

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            IWebHostBuilder hostBuilder = WebHost.CreateDefaultBuilder(args);
            return hostBuilder.ConfigureAppConfiguration((context, builder) =>
                {
                    //param1:檔案所在目錄的物理路徑
                    //param2:該檔案是否可選
                    builder.AddKeyPerFile(context.HostingEnvironment.ContentRootPath, true);
                }).UseStartup<Startup>();
        }

 控制器如下:

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        private readonly IConfiguration _config;

        public TestController(IConfiguration configuration)
        {
            _config = configuration;
        }

        public string Get(string key)
        {            
            return _config.GetValue(key, "defaultValue");
        }
    }

 

 

五. 記憶體配置提供程式  AddInMemoryCollection

這個很簡單,直接上圖:

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            Dictionary<string, string> memoryCollection = new Dictionary<string, string>
            {
                {"id","1" },
                {"name","wjire" }
            };
            IWebHostBuilder hostBuilder = WebHost.CreateDefaultBuilder(args);
            return hostBuilder.ConfigureAppConfiguration((context, builder) =>
            {
                builder.AddInMemoryCollection(memoryCollection);
            }).UseStartup<Startup>();
        }

 

 

六.自定義配置提供程式

該示例演示瞭如果使用EF建立從資料庫(MySql)讀取配置的提供程式.

第一步:安裝 MySqlEF

第二步:建立資料庫表:

CREATE TABLE `myconfigs` (
  `Id` varchar(20) NOT NULL DEFAULT '',
  `value` varchar(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

第三步:建立實體類

    public class MyConfig
    {
        public string Id { get; set; }

        public string Value { get; set; }
    }

第四步:建立資料庫上下文

    public class MyConfigContext : DbContext
    {
        public MyConfigContext(DbContextOptions<MyConfigContext> options) : base(options)
        {
        }

        public DbSet<MyConfig> MyConfigs { get; set; }
    }

第五步:建立配置資料來源

    public class MyConfigSource : IConfigurationSource
    {
        private readonly Action<DbContextOptionsBuilder> _optionsAction;

        public MyConfigSource(Action<DbContextOptionsBuilder> optionsAction)
        {
            _optionsAction = optionsAction;
        }


        public IConfigurationProvider Build(IConfigurationBuilder builder)
        {
            return new MyConfigProvider(_optionsAction);
        }
    }

第六步:建立配置資料來源提供器

    public class MyConfigProvider : ConfigurationProvider
    {
        private Action<DbContextOptionsBuilder> OptionsAction { get; }

        public MyConfigProvider(Action<DbContextOptionsBuilder> optionsAction)
        {
            OptionsAction = optionsAction;
        }
        
        //從資料庫讀取配置
        public override void Load()
        {
            DbContextOptionsBuilder<MyConfigContext> builder = new DbContextOptionsBuilder<MyConfigContext>();
            OptionsAction(builder);
            using (MyConfigContext dbContext = new MyConfigContext(builder.Options))
            {
                //Data 是基類 ConfigurationProvider 的屬性,用來儲存配置資料來源的.
                Data = !dbContext.MyConfigs.Any()//判斷表是否有資料
                    ? CreateAndSaveDefaultValues(dbContext)//如果沒有資料,則新增一些資料,儲存到配置資料來源中.
                    : dbContext.MyConfigs.ToDictionary(c => c.Id, c => c.Value);//如果有資料,讀取出來,儲存到配置資料來源中.
            }
        }
        
        private static IDictionary<string, string> CreateAndSaveDefaultValues(MyConfigContext dbContext)
        {
            Dictionary<string, string> configValues = new Dictionary<string, string>
            {
                { "1", "refuge" },
                { "2", "36" },
                { "3", "chengdu" }
            };
            dbContext.MyConfigs.AddRange(configValues.Select(kvp => new MyConfig
            {
                Id = kvp.Key,
                Value = kvp.Value
            }).ToArray());
            dbContext.SaveChanges();
            return configValues;
        }
    }

第七步:建立擴充套件方法,對外公開自定義的資料提供程式

    public static class MyConfigExtension
    {
        public static IConfigurationBuilder AddCustomConfigurationProviderApp(this IConfigurationBuilder builder, Action<DbContextOptionsBuilder> optionsAction)
        {
            return builder.Add(new MyConfigSource(optionsAction));
        }
    }

第八步:建立測試用的控制器

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        private readonly IConfiguration _config;

        public TestController(IConfiguration configuration)
        {
            _config = configuration;
        }
        
        //查詢所有
        public IEnumerable<KeyValuePair<string, string>> GetAll()
        {
            return _config.AsEnumerable();
        }

        //查詢某個key
        public string Get(string key)
        {
            return _config.GetValue(key, "defaultValue");
        }
    }

第九步:呼叫自定義配置提供程式

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {

            IWebHostBuilder hostBuilder = WebHost.CreateDefaultBuilder(args);
            return hostBuilder.ConfigureAppConfiguration((context, builder) =>
                {
                    builder.AddCustomConfigurationProviderApp(options => options.UseMySql("Server=localhost;Database=test;User=root"));
                }).UseStartup<Startup>();
        }
    }

測試結果:

1)查詢所有,可以看到,我們新增的配置資料已經加到系統中了;(看右邊的滾動條就知道總共有很多很多配置...)

2)查詢特定key

 

如果使用該方式提供配置資料,需要注意以下兩點:

1.提供程式在啟動時就將資料庫表讀入配置,不會基於每個key查詢資料庫;

2.應用啟動後,更新資料庫,配置不會更新.

 

上面講了如果提供配置資料,下面講獲取配置的幾種常用方法:

一.GetValue 上面已經演示過了,這裡不再重複.只把常用的過載方法列出來

GetValue<T>("key") 如果 key 不存在,則會拋異常;

GetValue<T>("key",T default) 如果 key 不存在,則返回預設值

 

二. T Get<T>()

對於下面這個 json 檔案:

{
  "name": "refuge",
  "age": 36, 
  "address": {
    "city": "chengdu" 
  } 
}

定義一個實體:

    public class Person
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public Address Address { get; set; }
    }

    public class Address
    {
        public string City { get; set; }
    }

控制器:

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        private readonly IConfiguration _config;

        public TestController(IConfiguration configuration)
        {
            _config = configuration;
        }
      
public string Get() { var person = _config.Get<Person>(); return JsonConvert.SerializeObject(person); } }

呼叫結果:

 

但是這種方式,個人持反對態度,因為儲存在系統中的配置資料有很多很多,上述方法相當於讀取所有配置資料來反序列化. 

 

三.GetSection

講該方法之前,有必要再熟悉一下配置在系統中的儲存結構到底是怎麼樣的.

對於上面那個 json 檔案:name": "refuge",

"age": 36, 
  "address": {
    "city": "chengdu" 
  } 
}

將其讀入配置後,儲存的結構如下:

{"key":"name","value":"refuge"}

{"key":"age","value":"36"}

{"key":"address","value":null} //通過查詢所有配置,確實有這一行,這意味著想通過 GetValue<string>("address") 反序列化為物件是不可能的...
{"key":"address:city","value":"chengdu"}]

對於上面這些配置,"refuge" 和 "36" 是可以通過第一個方法 GetValue<string>("name") , GetValue<string>("age") 來獲取.

 

 

 而 "chengdu" 只能用 GetValue<string>("address:city") 方法獲取了.

因此,要通過該方法把上述json檔案的內容轉成 Person 物件,則需要對 json 檔案進行修改,新增一個 "person" 節點:

{
  "person": {
    "name": "refuge",
    "age": 36,
    "address": {
      "city": "chengdu"
    }
  } 
}

Action:

        public string Get()
        {
            //方法一:
            {
                Person person = _config.GetSection("person").Get<Person>();
                return JsonConvert.SerializeObject(person);
            }

            //方法二:
            {
                //Person person = new Person();
                //_config.GetSection("person").Bind(person);
                //return JsonConvert.SerializeObject(person);
            }
        }

方法一明顯要帥氣得多!

問題又來了,如果我只想反序列化 "address" 節點的值,怎麼辦呢?下面這個方法可以完成.

 

四.GetChildren

Action:

        public string Get()
        {
                IConfigurationSection firstChildSection = _config.GetSection("person").GetChildren().First();
                Address address = firstChildSection.Get<Address>();
                return JsonConvert.SerializeObject(address);            
        }

 

 

 

貌似完了.