ASP.NET Core 注入和获取 AppSettings 配置
ASP.NET Core 项目中有个appsettings.json
配置文件,用于存放一些配置信息,比如数据库连接字符串等,但访问的话,只能在 ASP.NET Core 项目中获取,如果我们在其他项目类库中,该怎样获取呢?
实现方式就是利用 ASP.NET Core DI,将配置信息注入到 IoC 中,通过构造函数获取注入的对象。
appsettings.json
示例代码:
{
"AppSettings": {
"AccessKey": "111111",
"SecretKey": "22222",
"Bucket": "3333333",
"Domain": "http://wwww.domain.com"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Error",
"System": "Information",
"Microsoft": "Information"
}
}
}
对应AppSettings
对象代码:
public class AppSettings
{
public string AccessKey { get; set; }
public string SecretKey { get; set; }
public string Bucket { get; set; }
public string Domain { get; set; }
}
ConfigureServices
添加配置代码:
public void ConfigureServices(IServiceCollection services)
{
var appSettings = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettings);
services.AddTransient<IUpoladService, UpoladService>();
// Add framework services.
services.AddMvc();
}
UpoladService
通过构造函数方式获取注入对象:
public class UpoladService : IUpoladService
{
private AppSettings _appSettings;
public UpoladService(IOptionsMonitor<AppSettings> appSettings)
{
_appSettings = appSettings.CurrentValue; //IOptions 需要每次重新启动项目加载配置,IOptionsMonitor 每次更改配置都会重新加载,不需要重新启动项目。
}
}
参考资料:
ASP.NET Core 注入和获取 AppSettings 配置的更多相关文章
- Asp.Net Core 减少Controller获取重复注入对象
原文:Asp.Net Core 减少Controller获取重复注入对象 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012770274/art ...
- asp.net core重新加载应用配置
asp.net core重新加载应用配置 Intro 我把配置放在了数据库或者是Redis里,配置需要修改的时候我要直接修改数据库,然后调用一个接口去重新加载应用配置,于是就尝试写一个运行时重新加载配 ...
- ASP.NET Core应用程序的参数配置及使用(转载)
本文结构 提前准备 参数配置方式 appsettings.json 环境变量 命令行参数 在控制器中使用配置参数 注入IConfiguration对象 注入IOptions对象 总结 应用程序的开发不 ...
- asp.net core 教程(五)-配置
Asp.Net Core-配置 Asp.Net Core-配置 在这一章,我们将讨论 ASP.NET Core项目的相关的配置.在解决方案资源管理器中,您将看到 Startup.cs 文件.如果你有以 ...
- Linux CentOS7部署ASP.NET Core应用程序,并配置Nginx反向代理服务器
前言: 本篇文章主要讲解的是如何在Linux CentOS7操作系统搭建.NET Core运行环境并发布ASP.NET Core应用程序,以及配置Nginx反向代理服务器.因为公司的项目一直都是托管在 ...
- Hangfire&Autofac与ASP.NET CORE注入失败
Hangfire.Autofac与ASP.NET CORE注入失败 项目里面使用了Hangfire,因为之前没用过吧,遇到了个问题,就是使用了ico容器后,再用Hangfire总是注入不上对象,总是后 ...
- ASP.NET Core 在 JSON 文件中配置依赖注入
前言 在上一篇文章中写了如何在MVC中配置全局路由前缀,今天给大家介绍一下如何在在 json 文件中配置依赖注入. 在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等 ...
- ASP.NET Core DI 手动获取注入对象
ASP.NET Core DI 一般使用构造函数注入获取对象,比如在ConfigureServices配置注入后,通过下面方式获取: private IValueService _valueServi ...
- 转载:ASP.NET Core 在 JSON 文件中配置依赖注入
在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等)时候,都是提供了专有的接口以供使用第三方的依赖注入组件,比如我们常用的会使用 Autofac.Untiy.Stri ...
随机推荐
- leetcode98
class Solution { public: vector<int> V; void postTree(TreeNode* node) { if (node != NULL) { if ...
- 记号一下selenium+Firefox自动下载的参数
参考: https://blog.csdn.net/wxstar8/article/details/80782556 https://blog.csdn.net/xiaoguanyusb/articl ...
- [Shell]Bash变量:自定义变量 & 环境变量 & 位置参数变量 & 预定义变量
--------------------------------------------------------------------------------- 变量是计算机内存的单元,其中存放的值 ...
- win10系统goole浏览器安装postMan插件
1. 首先是下载PostMan工具,可以通过谷歌插件网站查询下载postman插件工具.解压文件 2. 解压压缩包 3. 修改_metadata文件重命名为metadata文件,保存待用.修改后为: ...
- 168. Excel Sheet Column Title (Math)
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- HDU 6304 Chiaki Sequence Revisited
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6304 多校contest1 Problem Description Chiaki is int ...
- 手机连得上WIFI,电脑连不上的情况
可以搜到,密码也对,但就是连不上,这时候可能就是你的设置错了. 操作步骤以下: 右击我的电脑-->管理-->设备管理器-->网络适配器-->找到你wifi对应的那个名称(如果不 ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 用php获取js变量的值
<script type="text/javascript"> var t1 = "fff"; var t2 = "<?php ec ...
- springboot + @KafkaListener 手动提交及消费能力优化
转载 https://blog.csdn.net/asd5629626/article/details/82776450 https://blog.csdn.net/asd5629626/artic ...