zoukankan      html  css  js  c++  java
  • ASP.NET Core 读取配置文件信息

    一:读取配置文件

    先来看一下appsettings.json文件的内容,如下图:

    {
      "ConnectionStrings": {
        "ServerConnection": "Data Source=users.db;",
        "BaseWebContext": "Server=.; database=BaseWebApp;uid=sa;pwd=@sa123;",
        "RedisConnection": "127.0.0.1:6380,password=123"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "Redis": {
        "ConnectionString": "127.0.0.1:6380,password=123"
      },
      "MongoDB": {
        "ConnectionString": "mongodb://127.0.0.1"
      },
      "AllowedHosts": "*",
      "Url": "http://*:8088"
    }

    接下来主要是实现如何获取ConnectionStrings和Redis节点下面的链接字符串信息

    1.新建ConfigurationManager.cs管理类

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Options;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace GyouApi
    {
        public static class ConfigurationManager
        {
            public readonly static IConfiguration Configuration;
    
            static ConfigurationManager()
            {
                Configuration = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                   .Build();
            }
    
            public static T GetSection<T>(string key) where T : class, new()
            {
                return new ServiceCollection()
                    .AddOptions()
                    .Configure<T>(Configuration.GetSection(key))
                    .BuildServiceProvider()
                    .GetService<IOptions<T>>()
                    .Value;
            }
    
            public static string GetSection(string key)
            {
                return Configuration.GetValue<string>(key);
            }
        }
    }

    2.获取ConnectionStrings节点下面的配置

    分别获取BaseWebContext 和RedisConnection

    private static readonly string BaseWebContextString = ConfigurationManager.Configuration.GetConnectionString("BaseWebContext")
    
    private static readonly string RedisConnectionString = ConfigurationManager.Configuration.GetConnectionString("RedisConnection");

    3.获取Redis节点下面的配置

     获取Redis的ConnectionString,  MongoDB下的同理

    
    
    //Redis节点
    private static readonly string RedisConnectionStringR = ConfigurationManager.GetSection("Redis:ConnectionString");
    //ConnectionString节点
    private static readonly string RedisConnectionString = ConfigurationManager.Configuration.GetConnectionString("RedisConnection");
  • 相关阅读:
    JS中短路运算符&&和||
    jadx 调整jvm参数
    python 面向对象(进阶篇)
    Python 面向对象(初级篇)
    python+requests接口自动化测试实战
    Jmeter-接口压测
    敬请指正-我进行单元测试的分享
    现在的开发都要懂测试了,为什么?这一份书单告诉你!
    你不得不看-自动化测试工程师面试总结:技术面试
    web自动化快速使用
  • 原文地址:https://www.cnblogs.com/JioNote/p/13666940.html
Copyright © 2011-2022 走看看