在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 非注入全局获取配置文件的更多相关文章

  1. SpringBoot获取配置文件,就这么简单。

    在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...

  2. Sping boot 之 @Value("${xxx}") 注解获取配置文件内容

    1.注解方式读取 1-1.@PropertySource配置文件路径设置,在类上添加注解,如果在默认路径下可以不添加该注解. 需要用@PropertySource的有: 例如非application. ...

  3. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  4. Spring中注解注入bean和配置文件注入bean

    注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...

  5. Spring Boot入门(二):获取配置文件值

    本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...

  6. .net core —— 控制台如何获取配置文件的内容?

    本文链接:https://blog.csdn.net/yenange/article/details/82457761参考: https://github.com/liuzhenyulive/Json ...

  7. .Net core 在类库中获取配置文件Appsettings中的值

    大多数情况,我们开发的程序中都含有很多个类库和文件夹,有时候,我们会遇到程序中的类库需要获取配置文件的信息的情况. 像dapper 中需要使用连接字符串的时候,那么我们一直从主程序中传值这是个不好的方 ...

  8. 在Spring应用中创建全局获取ApplicationContext对象

    在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...

  9. Mybatis 注入全局参数

    在项目中使用mybatis作为dao层,大部分时间都需要使用到mybatis提供的动态sql功能,一般情况下所有的表都是在同一个数据库下的,进行数据操作时都是使用jdbc中默认的schema.但是如果 ...

随机推荐

  1. zabbix 线路质量监控自定义python模块(Mysql版),多线程(后来发现使用协程更好)降低系统消耗

    之前零零碎碎写了一些zabbix 线路监控的脚本,工作中agnet较多,每条线路监控需求不一致,比较杂乱,现在整理成一个py模块,集合之前的所有功能 环境 python3.6以上版本,pip3(pip ...

  2. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

  3. unity---光照基础

    发射光源类型 光照参数介绍 让摄像头看到Flare 耀斑 改变影子

  4. Spring是如何整合JUnit的?JUnit源码关联延伸阅读

    上一篇我们回答了之前在梳理流程时遇到的一些问题,并思考了为什么要这么设计. 本篇是<如何高效阅读源码>专题的第十二篇,通过项目之间的联系来进行扩展阅读,通过项目与项目之间的联系更好的理解项 ...

  5. Ubuntu的一些软件源

    参考别人的,自己记录一下,怕丢失 修改方法:vim /etc/apt/sources.list,然后添加下面对应的代码区 台湾的官方源 deb http://tw.archive.ubuntu.com ...

  6. 个人冲刺(三)——体温上报app(二阶段)

    冲刺任务:完成用户类.温度数据和第二页面类的编写 User.java package com.example.helloworld; class User { private String usern ...

  7. python之生成器与模块

    目录 生成器对象 自定义range方法 生成器表达式 模块 简介 模块的导入方式 第一种:import ... 第二种:from ... import ... 补充 生成器对象 生成器对象其实本质还是 ...

  8. e2fsck-磁盘分区修复

    检查 ext2/ext3/ext4 类型文件系统. 语法 e2fsck [-panyrcdfvtDFV] [-b superblock] [-B blocksize] [-I inode_buffer ...

  9. MongoDB 常用启动参数

    每日一句 Once you choose your way of life, be brave to stick it out and never return. 生活的道路一旦选定,就要勇敢地走到底 ...

  10. axios的请求参数格式(get、post、put、delete)

    1.get请求方式: axios.get(url[, config]) // [字符拼接型]axios.get(url?id=123&status=0') // 等同于 axios.get(u ...