欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

.NET怎樣配置JSON中依賴注入

這篇文章主要介紹了.NET怎樣配置JSON中依賴注入,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)負(fù)責(zé)任的成都網(wǎng)站制作公司!

前言

在以前的 ASP.NET 4+ (MVC,Web Api,Owin,SingalR等)時(shí)候,都是提供了專有的接口以供使用第三方的依賴注入組件,比如我們常用的會(huì)使用 Autofac、Untiy、String.Net 等,這些第三放依賴注入組件基本上都提供了一套配置注入或者配置生命周期的方式,除了直接配置到類里面之外,還提供了要么使用 xml 文件,要么使用 json 等,那么在新的 ASP.NET Core 中微軟已經(jīng)默認(rèn)的給我們提供了一個(gè)依賴注入的功能,我們就不再需要借助于第三方組件來實(shí)現(xiàn)依賴注入了,但是有時(shí)候我們想在配置文件中來配置依賴注入,微軟本身的 DI 組件并沒有給我們提供一個(gè)可供配置的文件,那么我們就需要自己來實(shí)現(xiàn)這個(gè)配置項(xiàng)的功能。個(gè)人覺得其主要使用場景是一些在編譯時(shí)不能確定實(shí)現(xiàn)的,需要?jiǎng)討B(tài)修改實(shí)現(xiàn)的地方。

下面就來看看應(yīng)該如何來做這件事情吧。

Getting Started

首先,在應(yīng)用程序中我們創(chuàng)建一個(gè)接口,以供 DI使用:

public interface IFoo
{
  string GetInputString(string input);
}

然后,添加一個(gè) IFoo 接口的實(shí)現(xiàn) Foo

public class Foo : IFoo
{
  public string GetInputString(string input)
  {
    return $"輸入的字符串為:{ input }";
  }
}

接下來,我們需要把以上的 IFoo 接口和它的實(shí)現(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 方法,添加一個(gè)生命周期為瞬態(tài)的 IFoo 的實(shí)現(xiàn)。瞬態(tài)就是說在每次請(qǐng)求的時(shí)候都將創(chuàng)建一個(gè)Foo的實(shí)例。

以上是默認(rèn)微軟為我們提供的添加依賴注入的方法,下面我們來看一下怎么來改造成我們需要的使用 json 文件的方式。

使用 json 文件配置 DI

當(dāng)我們使用json文件配置依賴注入的時(shí)候,可以選擇新建一個(gè)json文件,也可以直接使用 appsettings.json 文件。現(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"
  }
 ]
}

首先,添加一個(gè)名為 “DIServices” 的數(shù)組節(jié)點(diǎn),數(shù)組中包含一個(gè)或多個(gè)配置service的對(duì)象,serviceType代表服務(wù)接口的類型,implementationType接口的實(shí)現(xiàn),lifetime 初始化實(shí)例的生命周期。

注意:配置文件中的類型必須為全名稱,即包含命名空間。

接下來,添加一個(gè)和Json文件配置項(xiàng)相對(duì)應(yīng)的一個(gè)service類,這里我們需要使用 Newtonsoft 這個(gè)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 ,添加注入項(xiàng):

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中使用。

運(yùn)行程序,打開頁面,點(diǎn)擊 About標(biāo)簽

.NET怎樣配置JSON中依賴注入

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“.NET怎樣配置JSON中依賴注入”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

標(biāo)題名稱:.NET怎樣配置JSON中依賴注入
文章起源:http://www.chinadenli.net/article38/jcocsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化軟件開發(fā)手機(jī)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈做網(wǎng)站App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化