1.首先在类库项目中添加

这3个库。

2.在类库项目中添加AppSetting.cs。代码如下:

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace EntityDemo
{
    public class AppSetting
    {
        private static readonly object objLock = new object();      //锁
        private static AppSetting instance = null;

private IConfigurationRoot Config { get; }
        //读取json文件
        private AppSetting()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            Config = builder.Build();
        }
        //加锁
        public static AppSetting GetInstance()
        {
            if (instance == null)
            {
                lock (objLock)
                {
                    if (instance == null)
                    {
                        instance = new AppSetting();
                    }
                }
            }

return instance;
        }
        //读取方法
        public static string GetConfig(string name)
        {
            return GetInstance().Config.GetSection(name).Value;
        }
    }
}

3.简单调用

3.1配置文件配置

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "TestDb": "server=localhost;port=3306;database=master;user=;password=;charset=utf8;"
  }
}

3.2类中调用

   public string TestConfig()
        {
            string str = AppSetting.GetConfig("ConnectionStrings:TestDb");
            return str;
        }

asp.net core 2.0类库项目读取配置文件的更多相关文章

  1. ASP.NET Core 2.0 : 三. 项目结构

    本章我们一起来对比着ASP.NET Framework版本看一下ASP.NET Core 2.0的项目结构.(此后的文章也尽量这样对比着, 方便学习理解.) 关注差异, 也为项目迁移做准备. 新建项目 ...

  2. ASP.NET Core实现类库项目读取配置文件

    前言 之前继续在学习多线程方面的知识,忽然这两天看到博问中有个园友问到如何在.net core类库中读取配置文件,当时一下蒙了,这个提的多好,我居然不知道,于是这两天了解了相关内容才有此篇博客的出现, ...

  3. ASP.NET Core 2.0 MVC项目实战

    一.前言 毕业后入职现在的公司快有一个月了,公司主要的产品用的是C/S架构,再加上自己现在还在学习维护很老的delphi项目,还是有很多不情愿的.之前实习时主要是做.NET的B/S架构的项目,主要还是 ...

  4. 使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流

    原文:使用Enablebuffering多次读取Asp Net Core 请求体 使用Enablebuffering多次读取Asp Net Core 请求体 1 .Net Core 2.X时代 使用E ...

  5. 在ASP.NET Core 2.0 web项目中使用EntityFrameworkCore

    一.安装EFCode包 EFCore需要根据不同的数据库选择不同的数据库提供程序database provider,各数据库的包地址:https://docs.microsoft.com/zh-cn/ ...

  6. 我来告诉你:VS2019开发ASP.NET Core 3.0 Web项目,修改视图后,刷新浏览器看不到修改后的效果怎么处理

    VisualStudio2019下一个2.2另一个3.0页面修改如下,但是3.0刷新没有任何变化,难道VS以后不能做前端开发了?大家可能没有看官方文档 根据文章所说你需要: 1.安装 Microsof ...

  7. 在ASP.NET Core中,静态类如何读取配置文件

    这是今天下午一个同事问我的问题,如何在静态类中读取json配置文件.我当时并没有告诉他如何如何去做,办法肯定是有,但是这种编程思维确实得改改了.静态类.静态方法不是面向对象编程的最佳实践..NET C ...

  8. ASP.NET Core 1.0 入门——了解一个空项目

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  9. 用ASP.NET Core 2.0 建立规范的 REST API -- 预备知识 (2) + 准备项目

    上一部分预备知识在这 http://www.cnblogs.com/cgzl/p/9010978.html 如果您对ASP.NET Core很了解的话,可以不看本文, 本文基本都是官方文档的内容. A ...

随机推荐

  1. bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装

    bundle install 老是中断,可以在gemfile里面把source换成taobao源,可以自动安装

  2. UVALIVE 4556 The Next Permutation

    4556 The Next PermutationFor this problem, you will write a program that takes a (possibly long) str ...

  3. C++中float类型的存储

    C++中float用32位来表示,f = (-1)^S * T * 2^E,S是符号位,T是尾数,E是指数 首先我们把f表示成科学计数法的形式,然后再写出其在内存中的表示,在这里T写成1.XXX的形式 ...

  4. Inversions SGU - 180

    这个是逆序对的裸题哇 归并排序或者树状数组~ 树状数组的话需要离散化一下··· emm确实很水很水很水··· 归并排序: #include <cstdio> #include <al ...

  5. Swiper thymeleaf

    html页面: <div class="swiper-container swiper_tjzt"> <div class="swiper-wrappe ...

  6. linux系统中的变量

    一.定义 所谓的变量,就是就是利用一个特定的"名称"(name)来存取一段可以变化的"值"(value),简单说来就是“用一个名称储存一个数值”.   二.设定 ...

  7. ubuntu 14.04 Clion2016.2 安装激活与安装后添加快捷启动方式

    参考链接:http://www.cnblogs.com/conw/p/5938113.html 下载clion for linux : http://www.jetbrains.com/clion/d ...

  8. Fix: The account is not authorized to log in from this station

    If you have more the one computers running Windows, then its possible to connect them using HomeGrou ...

  9. Greeplum 系列(六) 备份与恢复

    Greeplum 系列(六) 备份与恢复 http://www.dbdream.com.cn/category/greenplum/ 先介绍几个命令查看 Greenplum 集群状态: # 1. 查看 ...

  10. linux 下 使用wget 下载 jdk资源 命令

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...