亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Asp.net Core與類庫讀取配置文件信息的方法

 更新時間:2018年12月03日 11:28:08   作者:HOYU_Z  
這篇文章主要給大家介紹了關(guān)于Asp.net Core與類庫讀取配置文件信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

首先開一個腦洞,Asp.net core 被使用這么長時間了,但是關(guān)于配置文件(json)的讀取,微軟官方似乎并沒有給出像.net framework讀取web.config那樣簡單且完美。嚴(yán)重懷疑這是微軟為了促進(jìn).net core 生態(tài)繁榮搞的一點小手段。

appsetting.Development.json (appsetting.json的內(nèi)容和這個差不多,下面會講到多環(huán)境使用)

{
 "SettingPath": {
 "VideoFilePath": "C:\\Users\\89275\\Desktop\\Projects\\mv",
 "FfmpegPath": "C:/Users/89275/Desktop/Projects/mv/ffmpeg.exe",
 "FtpPath": "http://192.168.254.1/videofile",
 "VirtualPath": "/videoplay"
 },
 "RedisPath":"192.168.0.108:6379"
}

看了很多Asp.net core 讀取配置文件的博客,感覺都沒有很好的解決問題。

  • 最簡單的就是在StartUp中通過Configuration["SettingPath:VirtualPath"]的形式獲取信息;
  • 接下來就是在Controller中獲去配置文件信息,在控制器中讀取配置文件有兩種方法。

第一種是在controller初始化的時候把IHostingEnvironment,IConfiguration傳過來,然后把穿過來的值賦給controller中對應(yīng)的變量,酒后就可以正常讀取配置文件了(由于我是個菜逼,還沒看明白系統(tǒng)啟動的時候,這兩個變量是怎么傳給controller的)

  public class HomeController : Controller
 {
  //環(huán)境變量
  private readonly IHostingEnvironment hostingEnvironment;
  private IConfiguration Configuration;
  public HomeController(IHostingEnvironment hostingEnvironment, IConfiguration configuration)
  {
   this.hostingEnvironment = hostingEnvironment;
   Configuration = configuration;
  }

  pubilc void GetRedisPath()
  {
   string redisPath = Configuration["RedisPath"];
  }
 }

第二種是通過獲取對象的方式讀取配置文件,最近很多博客說的都是關(guān)于這個的。還是在controller初始化的時候把IOptions傳進(jìn)來(這里我還是沒懂怎么傳過來的/(ㄒoㄒ)/~~),然后把傳過來的值賦值給Model的對象,然后就可以正常使用了。

這種方法需要在StartUp中的ConfigureServices中有添加

   services.AddOptions();
   //SettingPath極為Model
   services.Configure<SettingPath>(Configuration.GetSection("SettingPath"));
 public class HomeController
 {

  public SettingPath settingPath;
  private ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(VideosController));
  public HomeController(IOptions<SettingPath> option)
  {
   settingPath = option.Value;
  }

  public void GetVideoPath()
  {
   string path=SettingPath.VideoFilePath
  }
 }

這里因為我不了解,IOptions是怎么傳進(jìn)來的,所以不知道如果有需要只用兩個或以上Model的情況該怎么處理。

.net core 讀取配置文件公共類

前面幾種方法之前都有用過,但是個人感覺用起來都不是很順手。而且如果想要在一個類庫中讀取配置文件的話簡直痛苦到不想理媳婦。

所以自己動手寫了一個工具類

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;

namespace Common
{
 public class ConfigurationHelper
 {
  public IConfiguration config { get; set; }
  public ConfigurationHelper()
  {
   IHostingEnvironment env = MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>();
   config = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables()
    .Build();
  }
  public T GetAppSettings<T>(string key) where T : class, new()
  {
   var appconfig = new ServiceCollection()
    .AddOptions()
    .Configure<T>(config.GetSection(key))
    .BuildServiceProvider()
    .GetService<IOptions<T>>()
    .Value;
   return appconfig;
  }
 }
 //我比較喜歡單獨放這個類,但是這樣放更明顯
 public class MyServiceProvider
 {
  public static IServiceProvider ServiceProvider { get; set; }
 }
}

使用這個類的話需要在StartUp的Configure中添加

 MyServiceProvider.ServiceProvider = app.ApplicationServices;

然后就可以在任何地方使用此類讀取配置文件信息了,而且由于ConfigurationHelper初始化時已經(jīng)默認(rèn)加載環(huán)境變量,所以同時具備多環(huán)境功能。

 string path = new ConfigurationHelper().config["RedisPath"];
   SettingPath pathss = new ConfigurationHelper().GetAppSettings<SettingPath>("SettingPath");

參考

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論