您好,登錄后才能下訂單哦!
這篇文章主要介紹ASP.NET Core在JSON文件中如何配置依賴注入,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
如何在在 json 文件中配置依賴注入。
在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等)時候,都是提供了專有的接口以供使用第三方的依賴注入組件,比如我們常用的會使用 Autofac、Untiy、String.Net 等,這些第三放依賴注入組件基本上都提供了一套配置注入或者配置生命周期的方式,除了直接配置到類里面之外,還提供了要么使用 xml 文件,要么使用 json 等,那么在新的 ASP.NET Core 中微軟已經(jīng)默認的給我們提供了一個依賴注入的功能,我們就不再需要借助于第三方組件來實現(xiàn)依賴注入了,但是有時候我們想在配置文件中來配置依賴注入,微軟本身的 DI 組件并沒有給我們提供一個可供配置的文件,那么我們就需要自己來實現(xiàn)這個配置項的功能。個人覺得其主要使用場景是一些在編譯時不能確定實現(xiàn)的,需要動態(tài)修改實現(xiàn)的地方。
下面就來看看應(yīng)該如何來做這件事情吧。
Getting Started
首先,在應(yīng)用程序中我們創(chuàng)建一個接口,以供 DI使用:
public interface IFoo { string GetInputString(string input); }
然后,添加一個 IFoo
接口的實現(xiàn) Foo
public class Foo : IFoo { public string GetInputString(string input) { return $"輸入的字符串為:{ input }"; } }
接下來,我們需要把以上的 IFoo
接口和它的實現(xiàn)添加到 Startup.cs 文件中的ConfigureServices
方法中,ConfigureServices 主要是用來配置依賴注入服務(wù)的。然后通過該方法提供的ISerciceCollection
接口參數(shù)注入 Services。
public void ConfigureServices(IServiceCollection services) { services.Add(new ServiceDescriptor(serviceType: typeof(IFoo), implementationType: typeof(Foo), lifetime: ServiceLifetime.Transient)); }
這里,我們使用到了 IServiceCollection 里面的 Add 方法,添加一個生命周期為瞬態(tài)的 IFoo
的實現(xiàn)。瞬態(tài)就是說在每次請求的時候都將創(chuàng)建一個Foo
的實例。
以上是默認微軟為我們提供的添加依賴注入的方法,下面我們來看一下怎么來改造成我們需要的使用 json 文件的方式。
使用 json 文件配置 DI
當我們使用json文件配置依賴注入的時候,可以選擇新建一個json文件,也可以直接使用 appsettings.json 文件?,F(xiàn)在我們就直接在 appsettings.json 文件中添加關(guān)于DI的配置了。
appsettings.json
"Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "DIServices": [ { "serviceType": "[namesapce].IFoo", "implementationType": "[namesapce].Foo", "lifetime": "Transient" } ] }
首先,添加一個名為 “DIServices” 的數(shù)組節(jié)點,數(shù)組中包含一個或多個配置service的對象,serviceType
代表服務(wù)接口的類型,implementationType
接口的實現(xiàn),lifetime
初始化實例的生命周期。
注意:配置文件中的類型必須為全名稱,即包含命名空間。
接下來,添加一個和Json文件配置項相對應(yīng)的一個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的構(gòu)造函數(shù)添加IFoo接口,然后在 About 的Action中使用。
運行程序,打開頁面,點擊 About標簽
以上是“ASP.NET Core在JSON文件中如何配置依賴注入”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。