netcore 非注入全局获取配置文件
在netcore开发中,最常见的就是注入,比如想获取appsettings.json的内容,我们就需要去注入,然后在controller里面去获取,但是我们如果想要在service中使用appsettings.json的内容,这样就是一个问题,并且每个controller去注入也是非常麻烦的事情
下面的注入的(这种方法百度一下可以出来几百条相同的搜索结果。。。参见https://www.cnblogs.com/ideacore/p/6282926.html)
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
然后获取使用

我想要在service类库里面使用,这时该如何使用哪?
直接上代码:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"AppSettings": {
"TestString": "This is default environment",
"ConfigVersion": "local",
"connectionString": "connectionString",
"RedisExchangeHosts": "RedisExchangeHosts"
}
}
public class AppSettings
{
public string TestString { get; set; }
public string ConfigVersion { get; set; }
public string connectionString { get; set; }
public string RedisExchangeHosts { get; set; }
public string UploadPath { get; set; }
}
public Startup(IConfiguration configuration, ILoggerFactory factory, IHostingEnvironment env)
{
EnvironmentName = env.EnvironmentName;
Configuration = configuration;
// 将内置的日志组件设置为 NHibernate 的日志组件
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)//增加环境配置文件,新建项目默认有
.AddEnvironmentVariables(); new AppSettingProvider().Initial(configuration); Configuration = builder.Build(); }
public class AppSettingProvider
{
private static AppSettings _myappSettings;
public static AppSettings _appSettings { get { return _myappSettings; } } public void Initial(IConfiguration configuration)
{
_myappSettings = new AppSettings() {
ConfigVersion = configuration["AppSettings:ConfigVersion"],
connectionString = configuration["AppSettings:connectionString"],
TestString = configuration["AppSettings:TestString"],
RedisExchangeHosts = configuration["AppSettings:RedisExchangeHosts"],
UploadPath = configuration["AppSettings:UploadPath"]
};
} }
这样,我们在要使用的时候只需要AppSettingProvider._appSettings.xxxx即可,不需要进行重复的、实现
有错误或者片面地方欢迎指正
netcore 非注入全局获取配置文件的更多相关文章
- SpringBoot获取配置文件,就这么简单。
在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...
- Sping boot 之 @Value("${xxx}") 注解获取配置文件内容
1.注解方式读取 1-1.@PropertySource配置文件路径设置,在类上添加注解,如果在默认路径下可以不添加该注解. 需要用@PropertySource的有: 例如非application. ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- Spring Boot入门(二):获取配置文件值
本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...
- .net core —— 控制台如何获取配置文件的内容?
本文链接:https://blog.csdn.net/yenange/article/details/82457761参考: https://github.com/liuzhenyulive/Json ...
- .Net core 在类库中获取配置文件Appsettings中的值
大多数情况,我们开发的程序中都含有很多个类库和文件夹,有时候,我们会遇到程序中的类库需要获取配置文件的信息的情况. 像dapper 中需要使用连接字符串的时候,那么我们一直从主程序中传值这是个不好的方 ...
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...
- Mybatis 注入全局参数
在项目中使用mybatis作为dao层,大部分时间都需要使用到mybatis提供的动态sql功能,一般情况下所有的表都是在同一个数据库下的,进行数据操作时都是使用jdbc中默认的schema.但是如果 ...
随机推荐
- 用c语言调用Easy X实现图像的输出,附带音乐的读取
要实现此功能需要用EasyX 一.下载VS编译环境和EasyX. ①Vs2019https://iwx.mail.qq.com/ftn/download?func=3&key=9e9b6c33 ...
- 69. Sqrt(x) - LeetCode
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
- Spring Boot配置全局异常捕获
1 SpringBoot配置全局的异常捕获 项目的说明 配置thymeleaf作为视图模板 ExceptionController.java模拟测试用 MyAjaxExceptionHandler.j ...
- 《Unix 网络编程》15:Unix 域协议
Unix 域协议 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 本 ...
- Chrome自带功能实现网页截图
更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月28日. 很简单,按下Ctrl+Shift+P,打开命令行窗口,如下图所示. 输入命令. Capture full size sc ...
- 02 RESTFul接口和HTTP的幂等性分析
RESTFul接口和HTTP的幂等性分析 REST全称是Representational State Transfer,中文为表述性状态转移,REST指的是一组架构约束条件和原则 RESTful表述的 ...
- Python Pygal 模块安装和使用你get了吗?
Pygal 是另一个简单易用的数据图库,它以面向对象的方式来创建各种数据图,而且使用 Pygal 可以非常方便地生成各种格式的数据图,包括 PNG.SVG 等.使用 Pygal 也可以生成 XML e ...
- POI导出复杂Excel,合并单元格(2)
/** * 导出excel (HSSFWorkbook) */ @GetMapping("/testExport") public void testExport1(HttpSer ...
- NC204859 组队
NC204859 组队 题目 题目描述 你的团队中有 \(n\) 个人,每个人有一个能力值 \(a_i\),现在需要选择若干个人组成一个团队去参加比赛,由于比赛的规则限制,一个团队里面任意两个人能力的 ...
- .Net 应用考虑x64生成
在x86架构下使用32位应用程序,理论上32位能够分配4G的大小空间,但实际上默认 .NET Framework 可以创建一个不超过 2 GB 的对象,在实际使用过程中可能使用到1.5G的样子就会报内 ...