媒介
在上一篇文章中写了如安在MVC中设置全局路由前缀,本日给人人引见一下如安在在 json 文件中设置依靠注入。
在之前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等)时刻,都是供应了专有的接口以供运用第三方的依靠注入组件,比方我们经常使用的会运用 Autofac、Untiy、String.Net 等,这些第三放依靠注入组件基本上都供应了一套设置注入或许设置生命周期的体式格局,除了直接设置到类内里以外,还供应了要么运用 xml 文件,要么运用 json 等,那末在新的 ASP.NET Core 中微软已默许的给我们供应了一个依靠注入的功用,我们就不再须要借助于第三方组件来完成依靠注入了,然则有时刻我们想在设置文件中来设置依靠注入,微软自身的 DI 组件并没有给我们供应一个可供设置的文件,那末我们就须要本身来完成这个设置项的功用。个人以为其重要运用场景是一些在编译时不能肯定完成的,须要动态修正完成的处所。
下面就来看看应当怎样来做这件事变吧。
Getting Started
起首,在应用顺序中我们建立一个接口,以供 DI运用:
public interface IFoo { string GetInputString(string input); }
然后,增加一个 IFoo
接口的完成 Foo
public class Foo : IFoo { public string GetInputString(string input) { return $"输入的字符串为:{ input }"; } }
接下来,我们须要把以上的 IFoo
接口和它的完成增加到 Startup.cs 文件中的ConfigureServices
要领中,ConfigureServices 重如果用来设置依靠注入效劳的。然后经由过程该要领供应的ISerciceCollection
接口参数注入 Services。
public void ConfigureServices(IServiceCollection services) { services.Add(new ServiceDescriptor(serviceType: typeof(IFoo), implementationType: typeof(Foo), lifetime: ServiceLifetime.Transient)); }
这里,我们运用到了 IServiceCollection 内里的 Add 要领,增加一个生命周期为瞬态的 IFoo
的完成。瞬态就是说在每次要求的时刻都将建立一个Foo
的实例。
以上是默许微软为我们供应的增加依靠注入的要领,下面我们来看一下怎样来改形成我们须要的运用 json 文件的体式格局。
运用 json 文件设置 DI
当我们运用json文件设置依靠注入的时刻,能够挑选新建一个json文件,也能够直接运用 appsettings.json 文件。如今我们就直接在 appsettings.json 文件中增加关于DI的设置了。
appsettings.json
"Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "DIServices": [ { "serviceType": "[namesapce].IFoo", "implementationType": "[namesapce].Foo", "lifetime": "Transient" } ] }
起首,增加一个名为 “DIServices” 的数组节点,数组中包括一个或多个设置service的对象,serviceType
代表效劳接口的范例,implementationType
接口的完成,lifetime
初始化实例的生命周期。
注重:设置文件中的范例必需为全称号,即包括定名空间。
接下来,增加一个和Json文件设置项相对应的一个service类,这里我们须要运用 Newtonsoft 这个json库。
using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Converters; public class Service { public string ServiceType { get; set; } public string ImplementationType { get;set; } [JsonConverter(typeof(StringEnumConverter))] public ServiceLifetime Lifetime { get; set; } }
然后须要革新一下ConfigureServices,在 ConfigureServices
中读取设置的 json文件即可。
public void ConfigureServices(IServiceCollection services) { //services.Add(new ServiceDescriptor(serviceType: typeof(IFoo), // implementationType: typeof(Foo), // lifetime: ServiceLifetime.Transient)); var jsonServices = JObject.Parse(File.ReadAllText("appSettings.json"))["DIServices"]; var requiredServices = JsonConvert.DeserializeObject<List<Service>>(jsonServices.ToString()); foreach (var service in requiredServices) { services.Add(new ServiceDescriptor(serviceType: Type.GetType(service.ServiceType), implementationType: Type.GetType(service.ImplementationType), lifetime: service.Lifetime)); } }
然后我们测试一下是不是是可用的。
测试
翻开 HomeController.cs
,增加注入项:
public class HomeController : Controller { private readonly IFoo _foo; public HomeController(IFoo foo) { _foo = foo; } public IActionResult About() { ViewData["Message"] = _foo.GetInputString("Your application description page."); return View(); } }
在 HomeController的组织函数增加IFoo接口,然后在 About 的Action中运用。
运转顺序,翻开页面,点击 About标签
总结
以上即为在 ASP.NET Core 中设置依靠注入到json文件中,这只是一个简朴的实例,不要用在生产环境中。在现实的项目中你还须要处置惩罚关于读取设置异常情况,效劳是不是存在的异常情况,生命周期等等这些题目。
【相干引荐】
1. 迥殊引荐:“php顺序员工具箱”V0.1版本下载
2. ASP免费视频教程
3. 李炎恢ASP基本视频教程
以上就是.NET设置 JSON 中依靠注入的要领引见的细致内容,更多请关注ki4网别的相干文章!