给IConfiguration写一个GetAppSetting扩展方法
给 IConfiguration 写一个 GetAppSetting 扩展方法
Intro
在 .net core 中,微软已经默认使用 appsettings.json 来代替 app.config,并重新设计了一套完整的配置系统,可以支持 json/xml/ini/环境变量等。
在 .net core 中有一个 GetConnectionString 的扩展方法用来比较方便的获取链接字符串,类似于在 .net framework 中使用 ConfigurationManager.ConnectionStrings["key"] 来获取链接字符串。
这次来参考 GetConnectionString 实现一个 GetAppSetting,类似于 .net framework 中使用 ConfigurationManager.AppSettings 来获取配置值。
实现代码
GetConnectionString 是获取 ConnectionStrings 这个节点下的某个配置,
GetAppSetting 来获取 AppSettings 这个节点下的某个配置
实现代码:
/// <summary>
/// GetAppSetting
/// Shorthand for GetSection("AppSettings")[key]
/// </summary>
/// <param name="configuration">IConfiguration instance</param>
/// <param name="key">appSettings key</param>
/// <returns>app setting value</returns>
public static string GetAppSetting([NotNull]this IConfiguration configuration, string key)
{
return configuration.GetSection("AppSettings")[key];
}
/// <summary>
/// GetAppSetting
/// Shorthand for GetSection("AppSettings")[key]
/// </summary>
/// <param name="configuration">IConfiguration instance</param>
/// <param name="key">appSettings key</param>
/// <returns>app setting value</returns>
public static T GetAppSetting<T>([NotNull]this IConfiguration configuration, string key)
{
return configuration.GetSection("AppSettings")[key].To<T>();
}
/// <summary>
/// GetAppSetting
/// Shorthand for GetSection("AppSettings")[key]
/// </summary>
/// <param name="configuration">IConfiguration instance</param>
/// <param name="key">appSettings key</param>
/// <param name="defaultValue">default value if not exist</param>
/// <returns>app setting value</returns>
public static T GetAppSetting<T>([NotNull] this IConfiguration configuration, string key, T defaultValue)
{
return configuration.GetSection("AppSettings")[key].ToOrDefault(defaultValue);
}
/// <summary>
/// GetAppSetting
/// Shorthand for GetSection("AppSettings")[key]
/// </summary>
/// <param name="configuration">IConfiguration instance</param>
/// <param name="key">appSettings key</param>
/// <param name="defaultValueFunc">default value func if not exist to get a default value</param>
/// <returns>app setting value</returns>
public static T GetAppSetting<T>([NotNull] this IConfiguration configuration, string key, Func<T> defaultValueFunc)
{
return configuration.GetSection("AppSettings")[key].ToOrDefault(defaultValueFunc);
}
使用
使用起来和 GetConnectionString 差不多
测试 appsettings.json
{
"ConnectionStrings": {
"TestDb": "server=.;database=Test;uid=weihanli;pwd=Admin888"
},
"AppSettings":{
"Number": 12,
"City": "Shanghai"
}
}
GetAppSetting 示例
IConfiguration configuration = new ConfigurationBuilder()
// ...
.AddJsonFile("appsettings.json")
.Build();
var city = configuration.GetAppSetting("City");
var number = configuration.GetAppSetting<int>("Number");
System.Console.WriteLine($"City:{city}, Number:{number}");
Memo
你可以复制上面的代码在你自己的代码里使用,也可以直接使用 WeihanLi.Common 这一 nuget 包
给IConfiguration写一个GetAppSetting扩展方法的更多相关文章
- 教你如何写一个 Yii2 扩展
前言 把一系列相关联的功能使用模块开发,好处多多,维护起来很方便,模块还可以单独发布出去,让下一个项目之间使用,真是方便. 下面我就写一个开发扩展的简单教程. Gii gii 自带帮助我们生成一个基本 ...
- 一个利用扩展方法的实例:AttachDataExtensions
扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...
- 自己写一个chrome扩展程序 - 右键菜单扩展
最近在学习Spring,心想dotnet如何实现类似形式呢.于是想认真学习Casetle组件,发现没有书籍!而spring的书多得很.于是只好找网上教程了.发现系统的文章不多.Terrylee好多文章 ...
- 给 string 添加一个 GetInputStream 扩展方法
有时候,我们须要读取一些数据,而无论这数据来源于磁盘上的数据文件,还是来源于网络上的数据.于是.就有了以下的 StringExtensions.cs: using System; using Syst ...
- 跟我一起写一个chrome扩展程序
在我没有看这本书之前,我都想象不到,原来chrome扩展程序可以这样写,真的非常有意思. 就是用最简单最基础的代码,然后就实现了一些非常有意思的玩意儿. 先看效果图 实际运用要和现实联系在一起,经历和 ...
- 如何通过写一个chrome扩展启动本地程序
@(编程) [toc] 本文介绍如何利用Chrome 的插件, 从我们的一个网站中启动一个我们的本地程序.本文的环境是windows10,本文的例子是通过点击网页上的一个button,调用本地的wor ...
- 怎样手写一个Object.create()方法
Object.create()会将参数对象作为一个新创建的空对象的原型, 并返回这个空对象, 基于这个功能, 就有了下面这个Object.create()的手动实现: function _create ...
- C# 向IQueryable添加一个Include扩展方法
using System; using System.Data.Objects; using System.Linq; namespace OutOfMemory.Codes { /// <su ...
- 添加一个js扩展方法
String.prototype.repeatify=String.prototype.repeatify || function(times){ var str=''; for(var i=0;i& ...
随机推荐
- [Swift]LeetCode43. 字符串相乘 | Multiply Strings
Given two non-negative integers num1 and num2 represented as strings, return the product of num1and ...
- [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- [Swift]LeetCode670. 最大交换 | Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- SpringMVC+JWT+Swagger UI+RestFul
前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...
- Python内置函数(14)——delattr
英文文档: delattr(object, name) This is a relative of setattr(). The arguments are an object and a strin ...
- "人机"对战:电脑太简单了,我是射手 skr~skr~skr
9月17日,2018 世界人工智能大会在上海拉开帷幕.在 SAIL 榜单入围项目中,我看到了小爱同学.小马智行.微软小冰.腾讯觅影等等,这不仅让我大开了眼界,也不禁让我感慨 AI 的发展神速.犹记得去 ...
- wp8使用Beetle.NetPackage实现基于TCP通讯的订单查询
在新版本的Beetle.NetPackage中提供了对Protobuf和Controller的支持,所以在WP8下使用Beetle.NetPackage进行基于TCP的数据交互则一件非常简单事情.下面 ...
- 15-Flink实战项目之实时热销排行
戳更多文章: 1-Flink入门 2-本地环境搭建&构建第一个Flink应用 3-DataSet API 4-DataSteam API 5-集群部署 6-分布式缓存 7-重启策略 8-Fli ...
- kubernetes的安装方法
背景 自己学习k8s集群,无奈屌丝一枚,没钱配置vpn服务,安装k8s花费的时间太久了.为了小伙伴们可以快速安装k8s,我花了点时间整理了这篇博客,提供一个不用FQ就可以愉快安装k8s集群的方法. 主 ...
- JavaScript与WebAssembly进行比较
本文由云+社区发表 作者:QQ音乐前端团队 在识别和描述核心元素的过程中,我们分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助 ...