您好,欢迎访问一九零五行业门户网

.NET Core配置文件加载与DI注入配置数据

.net core配置文件
在以前.net中配置文件都是以app.config / web.config等xml格式的配置文件,而.net core中建议使用以json为格式的配置文件,因为使用起来更加方面灵活,而且可以使用.net core中的di注入配置数据。
使用:
var config = new configurationbuilder() .addinmemorycollection() //将配置文件的数据加载到内存中 .setbasepath(directory.getcurrentdirectory()) //指定配置文件所在的目录 .addjsonfile("appsettings.json", optional: true, reloadonchange: true) //指定加载的配置文件 .build(); //编译成对象 console.writeline(config["test"]); //获取配置中的数据 config["test"] = "test test"; //修改配置对象的数据,配置对象的数据是可以被修改的 console.writeline(config["test11"]); //获取配置文件中不存在数据也是不会报错的 console.writeline(config["thekey:nextkey"]); //获取:thekey -> nextkey 的值
配置文件appsettings.json文件内容:
{ "test": "testval", "thekey": { "nextkey": "keyval" } }
注意:
configurationbuilder需要添加包:"microsoft.extensions.configuration"
addjsonfile需要添加包:"microsoft.extensions.configuration.json"
与di配合使用
var sp = new servicecollection() .addoptions() //注入ioptions<t>,这样就可以在di容器中获取ioptions<t>了 .configure<testcls>(config.getsection("testcls")) //注入配置数据 //也可以对注入的配置数据进行修改 .configure<testcls>(t => { t.name = "jame"; //修改name的值 }) .buildserviceprovider(); //编译 var test = sp.getservice<ioptions<testcls>>(); //获取注入的配置数据对象 console.writeline(jsonconvert.serializeobject(test.value)); //{"name":"jame","age":123} //下面的代码中检验configure注入的配置数据对象是单例模式的(.net core中di容器的三种生命周期:singleton(单例), scoped(作用域), transient(瞬态)) var test1 = sp.getservice<ioptions<testcls>>(); console.writeline(test == test1); //true //创建一个新的作用域获取配置数据对象 var test2 = sp.getservice<iservicescopefactory>().createscope().serviceprovider.getservice<ioptions<testcls>>(); console.writeline(test == test2); //true
配置测试类:
public class testcls { public string name { get; set; } public int age { get; set; } }
appsettings.json中的内容:
{ "testcls": { "name": "tom", "age": 123 } }
注意:
servicecollection需要添加包: "microsoft.extensions.dependencyinjection"
addoptions需要添加包: "microsoft.extensions.options.configurationextensions"
asp.net core中使用
startup.cs -> startup构造方法中进行初始化配置文件:
var builder = new configurationbuilder() .addinmemorycollection() .setbasepath(env.contentrootpath) .addjsonfile("appsettings.json", optional: true, reloadonchange: true) .addjsonfile($"appsettings.{env.environmentname}.json", optional: true); configuration = builder.build();
startup.cs -> configureservices方法中进行注入配置数据:
services.addoptions() //注入ioptions<t> .configure<testcls>(configuration.getsection(nameof(testcls))) .configure<testcls>(test => { test.name = "jame"; //修改name的值 });
配置文件中的配置数据:
{ "logging": { "includescopes": false, "loglevel": { "default": "debug", "system": "information", "microsoft": "information" } }, "testcls": { "name": "tom", "age": 123 } }
注入到控制器中:
[route("api/[controller]")] public class valuescontroller : controller { ioptions<testcls> _test; public valuescontroller(ioptions<testcls> test) { _test = test; } [httpget] public string gets() { return jsonconvert.serializeobject(_test.value); }
访问:/api/values
显示:{name:jame,age:123}
【相关推荐】
1. .net core 之 图形验证码
2. .net core cli工具文档dotnet-publish
3. 详细介绍zkeacms for .net core
4. 分享.net mvc中使用forms验证实例代码
5. 在.net core 下如何进行http请求?
6. centos上运行zkeacms的实例教程
其它类似信息

推荐信息