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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Core与类库读取配置⽂件信息的⽅法
前⾔
⾸先开⼀个脑洞, 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"
}
看了很多 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(, 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");
参考
https:///zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
https:///zh-cn/aspnet/core/fundamentals/environments?view=aspnetcore-2.1
https:///article/125674.htm
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。

相关文档
最新文档