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是 ...
随机推荐
- Linq 将两个查询结果合称为一个
var handsonitems = from a in db.DltQuestionHandson join c in db.DltBdChapter on new { a.ChapterCode ...
- 浅析libuv源码-node事件轮询解析(2)
上一篇讲了轮询的边角料,这篇进入正题.(竟然真有人看我博客,上两个图给你们整理下思路) 这是轮询总流程图. 下图为本节内容简图. Poll for I/O The loop blocks for I/ ...
- Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理
Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理 本文链接:https://blog.csdn.net/puhaiyang/article/details/78146620 ...
- digital clock based C
/********************************** * Name : timeDisplay.cpp * Purpose: Display digital clock accord ...
- HTML网页实现flv视频播放
一.HTML代码如下: <div id="player"></div> 二.JavaScript代码如下: <script src="htt ...
- oracle高级部分
回顾 多表关联查询的方式 内连接 根据AB表关联的条件进行过滤查询,只保留满足条件数据 Select * from a,b where a.xxx=b.xxx; Select * from a inn ...
- 【爬虫】网页抓包工具--Fiddler--Request和Response
[爬虫]网页抓包工具--Fiddler Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888, ...
- Kotlin开发springboot项目(一)
Kotlin开发springboot项目(一) Kotlin语言与Xtend语言有很多相似之处 为什么会存在这么多JVM语言? 现存的语言提供了太过受限制的功能,要不就是功能太过繁杂,导致语言的臃肿和 ...
- Github清除历史提交,保留最新提交
有时候,需要启动一个新的分支,同时想摒弃历史信息,那么可以使用下面的方法来实现 #克隆git仓库 git clone [URL] #进入git仓库 cd [仓库名] #创建一个名为 new_branc ...
- Linux(Centos7.6)下安装Gitlab详细教程
Gitlab搭建操作步骤: 1.查看Linux系统版本确认gitlab需要使用的安装包类型 使用命令:cat /etc/redhat-release CentOS Linux release 7.6. ...