当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net Core与类库读取配置文件信息的方法

Asp.net Core与类库读取配置文件信息的方法

2018年12月06日  | 移动技术网IT编程  | 我要评论

豪门罪妻,风云武诀,鸡西车祸

前言

首先开一个脑洞,asp.net core 被使用这么长时间了,但是关于配置文件(json)的读取,微软官方似乎并没有给出像.net framework读取web.config那样简单且完美。严重怀疑这是微软为了促进.net core 生态繁荣搞的一点小手段。

appsetting.development.json (appsetting.json的内容和这个差不多,下面会讲到多环境使用)

{
 "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中对应的变量,酒后就可以正常读取配置文件了(由于我是个菜逼,还没看明白系统启动的时候,这两个变量是怎么传给controller的)

  public class homecontroller : controller
 {
  //环境变量
  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"];
  }
 }

第二种是通过获取对象的方式读取配置文件,最近很多博客说的都是关于这个的。还是在controller初始化的时候把ioptions传进来(这里我还是没懂怎么传过来的/(ㄒ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是怎么传进来的,所以不知道如果有需要只用两个或以上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初始化时已经默认加载环境变量,所以同时具备多环境功能。

 string path = new configurationhelper().config["redispath"];
   settingpath pathss = new configurationhelper().getappsettings<settingpath>("settingpath");

参考

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网