.net core 根据环境变量区分正式、测试 配置文件
新建测试、正式环境下所用的 配置信息文件

appsettings.Development.json 文件信息:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"db": {
"mysql": {
"conStr": "xxxxxxxxxxxxxxxxx", //测试
"providerName": "MySql.Data.MySqlClient"
},
"redis": {
"conStr": "xxxxxxxxxxxxxxxxx", //测试
"dbId": "3"
}
},
"ThOcUrl": "xxxxxxxxxxxxxxxxx", //测试
"ThSftpInfo": "xxxxxxxxxxxxxxxxx", //测试
"ThFileUrl": "xxxxxxxxxxxxxxxxx" //测试
}
加入相关nuget引用

添加获取配置信息帮助类
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection; namespace YanWenCore.ThregGoods.Common
{
public static class MyServiceProvider
{
public static IServiceProvider ServiceProvider
{
get; set;
}
} public class AppSettingHelper
{
//根据环境变量配置获取json文件的名称
private static readonly string AppSettingName = String.Format("appsettings.{0}.json", MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>().EnvironmentName); private static IConfigurationRoot configR;
static AppSettingHelper()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(AppSettingName);
configR = builder.Build();
} public static IConfigurationRoot GetRotConfig()
{
if (configR == null)
{
configR = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(AppSettingName).Build();
}
return configR;
} //Mysql连接
public static string MysqlConnectionStringSettings
{
get
{
return GetRotConfig().GetSection("db").GetSection("mysql").GetSection("conStr").Value;
}
} //Redis连接信息
public static string RedisConnectionStringSettings
{
get
{
return GetRotConfig().GetSection("db").GetSection("redis").GetSection("conStr").Value;
}
} //OC平台接口地址
public static string GetbatteryCertUrl
{
get
{
return GetRotConfig().GetSection("ThOcUrl").Value;
}
} //sftp服务器信息
public static string ThSftpInfo
{
get
{
return GetRotConfig().GetSection("ThSftpInfo").Value;
}
} //文件服务器地址
public static string ThFileUrl
{
get
{
return GetRotConfig().GetSection("ThFileUrl").Value;
}
} } }
应用程序期间提供初始化提供服务容器的对象
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ app.UseExceptionHandler("/Error");
}
MyServiceProvider.ServiceProvider = app.ApplicationServices;
}
执行docker run 之前,设置环境变量
set ASPNETCORE_ENVIRONMENT= Development
.net core 根据环境变量区分正式、测试 配置文件的更多相关文章
- JAVA_SE基础——2.环境变量的配置&测试JDK
哈喽,利用晚上的空余时间再写篇心的~~~ 谢谢大家 前一篇文章 JAVA_SE基础--JDK&JRE下载及安装http://blog.csdn.net/thescript_j/article ...
- ASP.NET Core使用环境变量
前言 通常在应用程序开发到正式上线,在这个过程中我们会分为多个阶段,通常会有 开发.测试.以及正式环境等.每个环境的参数配置我们会使用不同的参数,因此呢,在ASP.NET Core中就提供了相关的环境 ...
- 在ASP.NET Core配置环境变量和启动设置
在这一部分内容中,我们来讨论ASP.NET Core中的一个新功能:环境变量和启动设置,它将开发过程中的调试和测试变的更加简单.我们只需要简单的修改配置文件,就可以实现开发.预演.生产环境的切换. A ...
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
0.背景 在开发项目的过程当中,生产环境与调试环境的配置肯定是不一样的.拿个最简单的例子来说,比如连接字符串这种东西,调试环境肯定是不能连接生产数据库的.在之前的话,这种情况只能说是你 COPY 两个 ...
- ASP.NET Core配置环境变量和启动设置
在这一部分内容中,我们来讨论ASP.NET Core中的一个新功能:环境变量和启动设置,它将开发过程中的调试和测试变的更加简单.我们只需要简单的修改配置文件,就可以实现开发.预演.生产环境的切换. A ...
- [转]ASP.NET Core配置环境变量和启动设置
本文转自:https://www.cnblogs.com/tdfblog/p/Environments-LaunchSettings-in-Asp-Net-Core.html 在这一部分内容中,我们来 ...
- .net core 中环境变量的配置
配置文件: Properties目录下的launchSettings.json IISExpress配置 "ASPNET_ENV": "EnvironmentOne&qu ...
- Web开发之tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
Tomcat的配置及测试: 第一步:下载tomcat,然后解压到任意盘符 第二步:配置系统环境变量 tomcat解压到的D盘 (路径为: D:\tomcat), 配置环境变量: 启动tomcat需要两 ...
- [.net core] 12.环境变量
异常给开发人员提供了很大好的排错功能. 但是其背后的技术细节很复杂,会损耗性能,也会使.net core web app更容易被反派攻击. 于是我们要学会使用环境变量, 通过环境变量,控制一些逻辑 当 ...
随机推荐
- vue组件级路由钩子函数(beforeRouteEnter/beforeRouteUpdate/beforeRouteLeave)
1.vue组件级路由钩子函数(beforeRouteEnter/beforeRouteUpdate/beforeRouteLeave):http://www.menvscode.com/detail/ ...
- 神器,阿里巴巴Java代码检查插件
背景 不久,又一气呵成发布了Eclipse/Intellij Idea下的代码检测插件PC3,可谓是国内代码优秀的检测插件.此插件检测的标准是根据<<阿里巴巴Java开发手册(终极版)&g ...
- 是否有任何python库可以从自然语言中解析日期和时间?
我正在寻找的是可以将“明天早上6点”或“中午的下一个模拟”转换为适当的日期时间对象. 解决方案 parsedatetime - 能够解析“人类可读”日期/时间表达式的Python模块. #!/usr/ ...
- stat - 打印信息节点(inode)内容
SYNOPSIS(总览) stat filename [filenames ... ] DESCRIPTION(描述) stat 打印出一个信息节点的内容,它们显示为对人可读的格式的stat(2). ...
- android thread Runnable
原文链接: http://blog.csdn.net/boyupeng/article/details/6208072 这篇文章中有三点需要提前说明一下, 一: 在android中有两种实现线程thr ...
- 论文学习——《Learning to Compose with Professional Photographs on the Web》 (ACM MM 2017)
总结 1.这篇论文的思路基于一个简单的假设:专业摄影师拍出来的图片一般具备比较好的构图,而如果从他们的图片中随机抠出一块,那抠出的图片大概率就毁了.也就是说,原图在构图方面的分数应该高于抠出来的图片. ...
- Codeforces 1175F 尺取法 性质分析
题意:给你一个数组,问有多少个区间,满足区间中的数构成一个排列. 思路(大佬代码):我们发现,一个排列一定含有1,所以我们不妨从1开始入手计算构成排列的区间个数.对于每个扫描到的1(假设处于位置i), ...
- python_django_admin
admin 是Django提供的基于web的管理工具,是系统管理员用于数据的输入,删除和查询的管理工具. 超级管理员在settings中的集成:INSTALLED_APPS=['django.cont ...
- Android 如何使edittext默认失去焦点
1.在布局文件中给edittext的父控件增加两个属性 android:focusable="true" android:focusableInTouchMode="tr ...
- leetcode-163周赛-1260-二维网格迁移
题目描述: 自己的提交: class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int] ...