前言:
還記得上篇文章中ASP.NET Core 依賴注入詳細最后提及到,假如服務(wù)越來越多怎么處理呢,本篇文章將會帶來解決辦法。這篇是接上一篇文章的,概念方面的可以參考上一篇文章。
一、IoC框架
先說說常見的Ioc框架吧。
Autofac:
目前net
用的比較多,好多大佬的項目比較優(yōu)先選擇的框架。
Ninject:
已經(jīng)很少用了,還時在很早的文章中見過。
Unity:
比較常見的了,好多地方有用到的,
Core: Core
中自帶的,業(yè)務(wù)邏輯不太復(fù)雜的情況下,還是比較方便的。
二、IoC-Autofac
Autofac
是.NET
領(lǐng)域最為流行的IOC
框架之一,傳說是速度最快的一個。
優(yōu)點:
-
它是C#語言聯(lián)系很緊密,也就是說C#里的很多編程方式都可以為
Autofac
使用。 -
較低的學(xué)習(xí)曲線,學(xué)習(xí)它非常的簡單,只要你理解了
IoC
和DI的概念以及在何時需要使用它們。 -
XML.Json
配置支持。 - 自動裝配。
-
與
Asp.Net MVC
集成。 -
微軟的
Orchad
開源程序使用的就是Autofac
,從該源碼可以看出它的方便和強大。
大多數(shù)講Autofac
框架的文章中都會提及上面幾點優(yōu)點,可見其框架的優(yōu)秀。
三、.NET Core中自帶DI的使用
1.首先創(chuàng)建一個 ASP.Net Core Web Api
項目(選用的.NET 5.0),整體如下,紅色部分為Core中自帶DI使用的部分。
2.新建類庫項目,分別添加一個接口文件和類文件。
接口:
public interface ISayHelloService { string SayHello(string name); }
類:
public class EnglishSayHelloService : ISayHelloService { public string SayHello(string name) { return $"Hello,{name}!"; } }
3.在 Startup
里面的 ConfigureServices
方法內(nèi)注入。
services.AddScoped<ISayHelloService, EnglishSayHelloService>();
4.然后在控制器中使用剛剛注入的服務(wù)。
[Route("api/[controller]/[action]")] [ApiController] public class HelloController : ControllerBase { public readonly ISayHelloService sayHelloService; public HelloController(ISayHelloService sayHelloService) { this.sayHelloService = sayHelloService; } [HttpGet] public string CoreDI() { return sayHelloService.SayHello("CoreDI"); } }
注意: 路由訪問地址,出現(xiàn)404錯誤時,可能是路由問題,路由可根據(jù)自己的實際需要自己在[Route("api/[controller]/[action]")]處修改。
5.訪問測試。
這里使用的接口測試軟件是Postman
,Api測試很方便,網(wǎng)上可以搜索到,找不到我到的可以聯(lián)系我。
四、Autofac 使用
1.在新建一個ASP.Net Core Web Api
項目(選用的.NET 5.0)用于區(qū)分前面的Core自帶的DI。
2.引用引用 Autofac
的包,看看這下載量,還是很哇塞的
3.在 Program
中改用 Autofac
來實現(xiàn)依賴注入
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
4.在 Startup 類中添加方法:ConfigureContainer
,注入我們之前的服務(wù)。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } //在這里注入 public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterType<EnglishSayHelloService>().As<ISayHelloService>(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
5.控制器這里基本不需要更改。
[Route("api/[controller]/[action]")] [ApiController] public class HelloController : ControllerBase { public readonly ISayHelloService sayHelloService; public HelloController(ISayHelloService sayHelloService) { this.sayHelloService = sayHelloService; } [HttpGet] public string CoreDI() { return sayHelloService.SayHello("AutofacDI"); } }
6.運行項目繼續(xù)用Postman測試接口。
好了關(guān)于Autofac
的基本使用基本就講完了,是不是還是挺簡單的。
五、批量注入
簡單的幾個服務(wù)寫著還可以接受,當(dāng)服務(wù)到幾十個,甚至上百個時,想想就可怕。這就不得不說到批量注入了,Autofac的優(yōu)勢就體現(xiàn)出來了。
1.在服務(wù)中分別在添加一個接口,和類。
接口:
public interface IUseAutofacService { string UseAutofac(string name); }
類:
public class UseAutofacService : IUseAutofacService { public string UseAutofac(string name) { return $"{name}批量注入測試!"; } }
2.回到我們之前的Startup 類中修改方法:ConfigureContainer
。
public void ConfigureContainer(ContainerBuilder builder) { //builder.RegisterType<EnglishSayHelloService>().As<ISayHelloService>(); //服務(wù)項目程序集 Assembly service = Assembly.Load("Autofac.Service"); //服務(wù)接口項目程序集 Assembly iservice = Assembly.Load("Autofac.Service"); builder.RegisterAssemblyTypes(service, iservice).Where(n => n.FullName.EndsWith("Service") && !n.IsAbstract) .InstancePerLifetimeScope().AsImplementedInterfaces(); }
注意: 如果需要注入的服務(wù)沒有 IXXXService
的接口 ,那么 builder.RegisterAssemblyTypes
就只需要傳一個程序集。如果服務(wù)與接口同在一個項目,那也是要傳兩個程序集的。
3.接下來在剛剛的控制器中略作修改。
[Route("api/[controller]/[action]")] [ApiController] public class HelloController : ControllerBase { public readonly ISayHelloService sayHelloService; public readonly IUseAutofacService useAutofacService; public HelloController(ISayHelloService _sayHelloService, IUseAutofacService _useAutofacService) { this.sayHelloService = _sayHelloService; this.useAutofacService = _useAutofacService; } [HttpGet] public string AutofacDI() { return sayHelloService.SayHello("AutofacDI"); } public string BathAutofacDI() { var name = sayHelloService.SayHello("AutofacDI"); return useAutofacService.UseAutofac(name); } }
4.用Postman
測試注入的情況。
到此這篇關(guān)于 ASP.NET Core 依賴注入框架的使用的文章就介紹到這了,更多相關(guān) ASP.NET Core 依賴注入框架內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/gurenyumao/p/15390167.html