netcore容器与配置文件操作
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System; namespace CoreConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//DI容器测试
var provider1 = new ServiceCollection()
.AddOptions()
.AddTransient<IPerson, Person>()
.AddTransient<IDog, Dog>()
.AddTransient<IFly, Fly>()
.BuildServiceProvider(); var dog1 = provider1.GetService<IDog>();
Console.WriteLine(provider1.GetService<IDog>().Name);
Console.WriteLine(provider1.GetHashCode()); var provider2 = new ServiceCollection()
.AddOptions()
.AddTransient<IPerson, Person>()
.AddTransient<IDog, Dog>()
.BuildServiceProvider();
var dog2 = provider2.GetService<IDog>();
Console.WriteLine(provider2.GetHashCode()); //配置文件读取测试
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.Add(new JsonConfigurationSource
{
Path = "appsettings.json",
Optional = false,
ReloadOnChange = true
}).Build();
ConfigHelper.config = config; var user = ConfigHelper.GetConfig<Usernfo>("User");
string url = ConfigHelper.GetValue("Url");
Console.WriteLine($"网址:{url}");
Console.WriteLine($"用户名:{user.Name},电话:{string.Join("|",user.Phones)}");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; namespace CoreConsoleApp1
{
public class ConfigHelper
{
public static IConfiguration config; public static string GetValue(string key)
{
return config.GetSection(key).Value;
} public static T GetConfig<T>(string key) where T : class, new()
{
var data = new ServiceCollection()
.AddOptions()
.Configure<T>(config.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value;
return data;
}
}
}
using System;
using System.Collections.Generic;
using System.Text; namespace CoreConsoleApp1
{
public interface IPerson
{
string Name { get; set; }
} public class Person : IPerson
{
public string Name { get; set; }
} public interface IDog
{
string Name { get; set; }
} public class Dog : IDog
{
private IFly _fly; public Dog()
{ } public Dog(IFly fly)
{
_fly = fly;
} public Dog(string name)
{
this.Name = name;
}
public string Name { get; set; } = "huahua";
} public interface IFly
{
string Fiy();
} public class Fly : IFly
{
public string Fiy()
{
return "fly....";
}
}
}
using System;
using System.Collections.Generic;
using System.Text; namespace CoreConsoleApp1
{
public class Usernfo
{
public string Name { get; set; }
public string[] Phones { get; set; }
}
}
appsettings.json
{
"Url": "http://www.test.com",
"User": {
"Name": "zhangsan",
"Phones": [ "", "", "" ]
}
}

netcore容器与配置文件操作的更多相关文章
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
IT咨询顾问:一次吐血的项目救火 年后的一个合作公司上线了一个子业务系统,对接公司内部的单点系统.我收到该公司的技术咨询:项目启动后没有规律的突然无法登录了,重新启动后,登录一断时间后又无法重新登 ...
- 一文了解Docker容器技术的操作
一文了解Docker容器技术的操作 前言一.Docker是什么二.Docker的安装及测试Docker的安装Docker的Hello world测试三.Docker的常见操作镜像的基本操作容器的基本操 ...
- 配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...
- centos7下安装docker(8.3容器的常用操作)
yu我们之前已经学习了如何运行容器docker run,也学习了如何进入容器docker attach和docker exec,下面我们来学习容器的其他操作: stop/start/restart 1 ...
- C# 配置文件操作类
注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; usin ...
- 修改SolrCloud在ZooKeeper中的配置文件操作记录
修改SolrCloud在ZooKeeper中的配置文件操作记录. 命令执行目录: /opt/solr-/server/scripts/cloud-scripts/ 1.下载配置文件 ./zkcli., ...
- .NET程序配置文件操作(ini,cfg,config)
在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...
- asp.netcore 深入了解配置文件加载过程
前言 配置文件中程序运行中,担当着不可或缺的角色:通常情况下,使用 visual studio 进行创建项目过程中,项目配置文件会自动生成在项目根目录下,如 appsettings.json, ...
- docker镜像、容器以及命令操作
docker image docker image是一个极度精简版的Linux程序运行环境,官网的java镜像包括的东西更少,除非是镜像叠加方式的如centos+java7 docker image是 ...
随机推荐
- 2019-11-29-WPF-如何在绑定失败异常
原文:2019-11-29-WPF-如何在绑定失败异常 title author date CreateTime categories WPF 如何在绑定失败异常 lindexi 2019-11-29 ...
- knockout.js绑定(enable,disable,visable)
<input type="text" data-bind="disable:IsNew" /> enable :是否可用,为true时,可编辑 di ...
- JAVA WEB面试总结
本文目录: 1. 什么是cookie 2. 什么是session 3.什么是Servlet,Servlet生命周期方法 4.JSP隐含对象 5.JSP的四个域对象的作用范围 6.转发和重定向的区别 7 ...
- WebSocket实现Web端即时通信
前言 WebSocket 是HTML5开始提供的一种在浏览器和服务器间进行全双工通信的协议.目前很多没有使用WebSocket进行客户端服务端实时通信的web应用,大多使用设置规则时间的轮询,或者使用 ...
- HighChat动态绑定数据 数据后台绑定(三)
今天看了几位大佬的博客,学到了一些,现在分享一下,也作为以后的参考 不多说看代码 1.后台代码 public ActionResult Ajax2() { ReportData reportData ...
- 实体类id的几种生成方式
@Id// @GeneratedValue(strategy = GenerationType.AUTO) // 自增// @GeneratedValue(strategy = GenerationT ...
- RC4弱密码套件检测
一.漏洞分析 事件起因 2015年3月26日,国外数据安全公司Imperva的研究员Itsik Mantin在BLACK HAT ASIA 2015发表论文<Attacking SSL when ...
- ECharts学习指南
1.了解ECharts ECharts简单说就是互联网开发程序过程中,后台数据库用以实现数据到图形的映射的一个插件. 具体来说一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在PC和 ...
- Quality Center安装步骤
测试管理工具 u 安装条件: Quality Center:QC9中文版 数据库:Oracle10 g 操作系统:Windows XP u 安装步骤: 1. 点击setup.exe,出现如下界面,点 ...
- Excel单元格锁定及解锁
Excel VBA 宏 学习使用: 一.工作表单元格的锁定: 1.选择需要锁定的单元格. 2.鼠标右键----设置单元格格式. 3.设置 “保护”--锁定 -- 确定. 4.回到表头,[审阅]--- ...