.NET中IoC框架Autofac用法講解
1 前置閱讀
在閱讀本文章之前,你可以先閱讀:
2 簡介
Autofac與C#語言的結(jié)合非常緊密,并學(xué)習(xí)它非常的簡單,也是.NET領(lǐng)域最為流行的IoC框架之一。
3 使用
首先,將Autofac的NuGet軟件包安裝到您的應(yīng)用程序中。
Autofac
然后,我們通過創(chuàng)建ContainerBuilder來注冊組件。
var builder = new ContainerBuilder(); builder.RegisterType<Knife>(); builder.RegisterType<Actor>();
接著,可以通過在一個已存在的生命周期上調(diào)用 BeginLifetimeScope() 方法來創(chuàng)建另一個生命周期作用域, 以根容器作為起始。生命周期作用域是可釋放的并且追蹤組件的釋放, 因此確保你總是調(diào)用了 "Dispose()"" 或者把它們包裹在 "using" 語句內(nèi)。
using (var scope = container.BeginLifetimeScope()) { }
最后,在注冊完組件并暴露相應(yīng)的服務(wù)后, 你可以從創(chuàng)建的容器或其子生命周期中解析服務(wù). 讓我們使用 Resolve() 方法來實現(xiàn):
using (var scope = container.BeginLifetimeScope()) { var actor = scope.Resolve<Actor>(); actor.Kill(); }
讓我們來看看完整代碼:
using System; namespace Autofac.ConsoleApp { class Program { static void Main(string[] args) { var builder = new ContainerBuilder(); builder.RegisterType<Knife>(); builder.RegisterType<Actor>(); var container = builder.Build(); using (var scope = container.BeginLifetimeScope()) { var actor = scope.Resolve<Actor>(); actor.Kill(); } Console.ReadKey(); } } }
讓我們來看看輸出結(jié)果:
小明用刀殺怪
4 在 Asp.Net Core 中使用
首先,將Autofac,Autofac.Extensions.DependencyInjection的NuGet軟件包安裝到您的應(yīng)用程序中。
dotnet add package Autofac dotnet add package Autofac.Extensions.DependencyInjection
然后,在Program.Main中增加.UseServiceProviderFactory(new AutofacServiceProviderFactory())
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseServiceProviderFactory(new AutofacServiceProviderFactory());
接著,在Startup.ConfigureServices中增加services.AddControllersWithViews();
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Autofac.WebApi", Version = "v1" }); }); services.AddControllersWithViews(); }
接著,在Startup.ConfigureContainer方法中,將Knife,Actor注冊到Autofac中ContainerBuilder。
public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterType<Knife>(); builder.RegisterType<Actor>(); }
最后,增加HomeController,執(zhí)行actor.Kill。
using Microsoft.AspNetCore.Mvc; using System; namespace Autofac.WebApi.Controllers { [Route("[controller]")] [ApiController] public class HomeController : Controller { private readonly Actor actor; public HomeController(Actor actor) { this.actor = actor ?? throw new ArgumentNullException(nameof(actor)); } [HttpGet] public string Get() { return actor.Kill(); } } }
啟動調(diào)試,讓我們來看看輸出結(jié)果:
小明用刀殺怪
到此這篇關(guān)于.NET中IoC框架Autofac用法講解的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解讀ASP.NET 5 & MVC6系列教程(3):項目發(fā)布與部署
這篇文章主要介紹了ASP.NET 5 項目發(fā)布與部署的設(shè)置和流程,并介紹IIS和web.cmd模式兩種發(fā)布模式的區(qū)別,需要的朋友可以參考一下。2016-06-06為Visual Studio手工安裝微軟ReportViewer控件
這篇文章介紹了為Visual Studio手工安裝微軟ReportViewer控件的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06在ASP.NET 2.0中操作數(shù)據(jù)之五十:為GridView控件添加Checkbox
本文主要介紹ASP.NET 2.0中為GridView控件添加Checkbox復(fù)選框控件的方法,并實現(xiàn)全選功能。2016-05-05ASP.NET中URL Routing和IIS上URL Rewriting的區(qū)別
這篇文章主要介紹了ASP.NET中URL Routing和IIS上URL Rewriting的區(qū)別,需要的朋友可以參考下。2016-06-06《解剖PetShop》之三:PetShop數(shù)據(jù)訪問層之消息處理
本文主要講解PetShop4.0的數(shù)據(jù)訪問層的消息處理部分,需要的朋友可以參考下。2016-05-05在ASP.NET 2.0中操作數(shù)據(jù)之四十一:DataList和Repeater數(shù)據(jù)分頁
DataList 和Repeater 都沒有提供內(nèi)置的分頁和排序功能,本文主要介紹利用PagedDataSource實現(xiàn)DataList和Repeater數(shù)據(jù)分頁。2016-05-05解讀ASP.NET 5 & MVC6系列教程(10):Controller與Action
這篇文章主要介紹了ASP.NET 5 Controller與Action的定義和使用,需要的朋友可以參考下2016-06-06.Net?Core微服務(wù)網(wǎng)關(guān)Ocelot集成Consul
這篇文章介紹了.Net?Core微服務(wù)網(wǎng)關(guān)Ocelot集成Consul的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01