1. 程式人生 > 實用技巧 >.NET 5中 Autofac 的使用

.NET 5中 Autofac 的使用

DI 依賴注入、IOC 控制反轉以及 AOP 切面程式設計

Autofac 容器

Autofac 是一款.NET IoC 容器 . 它管理類之間的依賴關係, 從而使 應用在規模及複雜性增長的情況下依然可以輕易地修改 . 它的實現方式是將常規的.net類當做 元件 處理.

  1. 安裝 NuGet 程式包: Autofac 6.0.0
  2. 建立一個 ContainerBuiler
  3. 註冊介面和實現關係
  4. 通過 ContainerBuiler 的 Build 方法,得到 IContainer 容器
  5. 通過 IContainer 容器獲取例項
  6. 使用服務
  • ITestServiceATestServiceA
	public interface ITestServiceA
    {
        void Show();
    }

    public class TestServiceA : ITestServiceA
    {
        public TestServiceA()
        {
            Console.WriteLine($"{this.GetType().Name} 被構造了...");
        }

        public void Show()
        {
            Console.WriteLine($"This is a {this.GetType().Name} Instance...");
        }
    }
  • Program 中的 Main 方法
 var builder = new ContainerBuilder();
 builder.RegisterType<TestServiceA>().As<ITestServiceA>();
 var container = builder.Build();

 // 獲取服務例項
 var testService = container.Resolve<ITestServiceA>();
 testService.Show();

Autofac 多種注入方式

  • ITestServiceBTestServiceB
    public interface ITestServiceB
    {
        void Show();
    }

    public class TestServiceB : ITestServiceB
    {
        private ITestServiceA _testServiceA;

        public void SetService(ITestServiceA testServiceA)
        {
            _testServiceA = testServiceA;
        }

        public TestServiceB()
        {
            Console.WriteLine($"{this.GetType().Name} 被構造了...");
        }

        public void Show()
        {
            // _testServiceA.Show();
            Console.WriteLine($"This is a {this.GetType().Name} Instance...");
        }
    }
  • ITestServiceCTestServiceC
    public interface ITestServiceC
    {
        void Show();
    }

    public class TestServiceC : ITestServiceC
    {
        public TestServiceC()
        {
            Console.WriteLine($"{this.GetType().Name} 被構造了...");
        }

        public void Show()
        {
            Console.WriteLine($"This is a {this.GetType().Name} Instance...");
        }
    }
  • ITestServiceDTestServiceD
    public interface ITestServiceD
    {
        void Show();
    }

    public class TestServiceD : ITestServiceD
    {
        public ITestServiceA TestServiceA { get; set; }
        public ITestServiceB TestServiceB { get; set; }
        public ITestServiceC TestServiceC { get; set; }

        public TestServiceD()
        {
            Console.WriteLine($"{this.GetType().Name} 被構造了...");
        }

        public void Show()
        {
            // TestServiceA.Show();
            // TestServiceB.Show();
            // TestServiceC.Show();
            Console.WriteLine($"This is a {this.GetType().Name} Instance...");
        }
    }
  1. 建構函式注入
 var builder = new ContainerBuilder();
 builder.RegisterType<TestServiceA>().As<ITestServiceA>();
 builder.RegisterType<TestServiceB>().As<ITestServiceB>();
 builder.RegisterType<TestServiceC>().As<ITestServiceC>();
 builder.RegisterType<TestServiceD>().As<ITestServiceD>();
 var container = builder.Build();

 // 獲取服務例項
 var testService = container.Resolve<ITestServiceA>();
 testService.Show();
  1. 屬性注入
 var builder = new ContainerBuilder();
 builder.RegisterType<TestServiceA>().As<ITestServiceA>();
 builder.RegisterType<TestServiceB>().As<ITestServiceB>();
 builder.RegisterType<TestServiceC>().As<ITestServiceC>();
 builder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();
 var container = builder.Build();

 // 獲取服務例項
 var testService = container.Resolve<ITestServiceD>();
 testService.Show();
  1. 方法注入
 var builder = new ContainerBuilder();
 builder.RegisterType<TestServiceA>().As<ITestServiceA>();
 builder.RegisterType<TestServiceB>().OnActivated(e => 
          e.Instance.SetService(e.Context.Resolve<ITestServiceA>())
 ).As<ITestServiceB>();
 builder.RegisterType<TestServiceC>().As<ITestServiceC>();
 builder.RegisterType<TestServiceD>().As<ITestServiceD>();
 var container = builder.Build();

 // 獲取服務例項
 var testService = container.Resolve<ITestServiceB>();
 testService.Show();

Autofac 生命週期

  1. InstancePerDependency :預設模式,每次呼叫,都會重新例項化物件;每次請求都建立一個新的物件;
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerDependency();
var container = builder.Build();
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceA1 = container.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1));
  1. SingleInstance :單例模式,每次呼叫,都會使用同一個例項化的物件;每次都用同一個物件;
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().SingleInstance();
var container = builder.Build();
var testServiceA = container.Resolve<ITestServiceA>();
var testServiceA1 = container.Resolve<ITestServiceA>();
Console.WriteLine(object.ReferenceEquals(testServiceA,testServiceA1));
  1. InstancePerLifetimeScope : 同一個生命週期域中,每次呼叫,都會使用同一個例項化的物件;每次都用同一個物件;且每個不同的生命週期域中的例項是唯一的,不共享的。
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerLifetimeScope();
var container = builder.Build();
ITestServiceA testServiceA15;
ITestServiceA testServiceA16;
using (var scope1 = container.BeginLifetimeScope())
{
    var testServiceA11 = scope1.Resolve<ITestServiceA>();
    var testServiceA12 = scope1.Resolve<ITestServiceA>();
    Console.WriteLine(object.ReferenceEquals(testServiceA11,testServiceA12));
    testServiceA15 = testServiceA12;
}
using (var scope1 = container.BeginLifetimeScope())
{
    var testServiceA13 = scope1.Resolve<ITestServiceA>();
    var testServiceA14 = scope1.Resolve<ITestServiceA>();
    Console.WriteLine(object.ReferenceEquals(testServiceA13,testServiceA14));
    testServiceA16 = testServiceA14;
}
Console.WriteLine(object.ReferenceEquals(testServiceA15,testServiceA16));
  1. InstancePerMatchingLifetimeScope : 同一個匹配的生命週期域中,每次呼叫,都會使用同一個例項化的物件;每次都用同一個物件;且每個不匹配的生命週期域中的例項是唯一的,不共享的。
var builder = new ContainerBuilder();
builder.RegisterType<TestServiceA>().As<ITestServiceA>()
    .InstancePerMatchingLifetimeScope("Run2948");
var container = builder.Build();
ITestServiceA testServiceA15;
ITestServiceA testServiceA16;
using (var scope1 = container.BeginLifetimeScope("Run2948"))
{
    var testServiceA11 = scope1.Resolve<ITestServiceA>();
    using (var scope2 = container.BeginLifetimeScope())
    {
        var testServiceA12 = scope2.Resolve<ITestServiceA>();
        Console.WriteLine(object.ReferenceEquals(testServiceA11,testServiceA12));
    }
    testServiceA15 = testServiceA11;
}
using (var scope1 = container.BeginLifetimeScope("Run2948"))
{
    var testServiceA13 = scope1.Resolve<ITestServiceA>();
    using (var scope2 = container.BeginLifetimeScope())
    {
        var testServiceA14 = scope2.Resolve<ITestServiceA>();
        Console.WriteLine(object.ReferenceEquals(testServiceA13,testServiceA14));
    }
    testServiceA16 = testServiceA13;
}
Console.WriteLine(object.ReferenceEquals(testServiceA15,testServiceA16));
  1. InstancePerOwned : 在一個所擁有的例項建立的生命週期中,每次呼叫,都會使用同一個例項化的物件;每次都用同一個物件;(較少使用)

  2. InstancePerHttpRequest : 同一次Http請求上下文中,每次呼叫,都會使用同一個例項化的物件;每次都用同一個物件;僅適用於 ASP.NET (CORE) MVC 或 WebForm 應用程式

Autofac 支援配置檔案

  1. 安裝 NuGet 程式包: Autofac.Extensions.DependencyInjection 7.1.0Autofac.Configuration 6.0.0
  2. 新建配置檔案(指定介面和實現的對應關係) autofac.json:
{
 	"components":[
        {
            "type: "One.Services.TestServiceA,One",
            "services": [
            	{
            		"type": "One.Services.ITestServiceA,One"
        		}
            ],
    		"instanceScope": "single-instance",
    		"injectProperties": true
        },
        {
            "type: "One.Services.TestServiceB,One",
            "services": [
            	{
            		"type": "One.Services.ITestServiceB,One"
        		}
            ],
    		"instanceScope": "single-instance",
    		"injectProperties": true
        },
        {
            "type: "One.Services.TestServiceC,One",
            "services": [
            	{
            		"type": "One.Services.ITestServiceC,One"
        		}
            ],
    		"instanceScope": "single-instance",
    		"injectProperties": true
        },
        {
            "type: "One.Services.TestServiceD,One",
            "services": [
            	{
            		"type": "One.Services.ITestServiceD,One"
        		}
            ],
    		"instanceScope": "single-instance",
    		"injectProperties": true
        }
    ]   
}
  1. 讀取配置檔案,完成服務對應關係的註冊
var builder = new ContainerBuilder();
var config = new ConfigurationBuilder();
var configSource = new JsonConfigurationSource()
{
    Path = "Config/autofac.json",
    Optional = false,
    ReloadOnChange = true
};
config.Add(configSource); 
var configModule = new ConfigurationModule(config.Build());
builder.RegisterModule(configModule);
var container = builder.Build();

 // 獲取服務例項
 var testServiceA = container.Resolve<ITestServiceA>();
 var testServiceD = container.Resolve<ITestServiceD>();
 testServiceD.Show();
  1. 新建 ITestServiceA 的新版實現類 TestServiceUpdate
public class TestServiceUpdate : ITestServiceA
{
    public TestServiceUpdate()
    {
        Console.WriteLine($"{this.GetType().Name} 被構造了...");
    }

    public void Show()
    {
        Console.WriteLine($"This is a {this.GetType().Name} Instance...");
    }
}
  1. 通過修改配置檔案 autofac.json 來實現快速實現 ITestServiceA 的實現的重新定義:
{
 	"components":[
        {
            "type: "One.Services.TestServiceUpdate,One",
            "services": [
            	{
            		"type": "One.Services.ITestServiceA,One"
        		}
            ],
    		"instanceScope": "single-instance",
    		"injectProperties": true
        },
		// ...

Autofac 整合 .NET 5 MVC

  1. 安裝 NuGet 程式包: Autofac.Extensions.DependencyInjection 7.1.0

  2. Program檔案中指定 Autofac 工廠替換預設工廠:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            }).UseServiceProviderFactory(new AutofacServiceProviderFactory());
  1. 在 Startup 類中增加 ConfigureContainer 方法:
	public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterType<TestServiceA>().As<ITestServiceA>();
    }
  1. 通過控制器建構函式注入,獲取例項
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ITestServiceA _serviceA;

        public ValuesController(ITestServiceA serviceA
        {
            _serviceA = serviceA;
        }

        [HttpGet]
        public IActionResult Get()
        {
            _serviceA.Show();
            return Ok();
        }
    }
  1. 使用 IServiceCollection 註冊的服務,將來也會交給 Autofac 管理
    public void ConfigureServices(IServiceCollection services)
    {
      #region IServiceCollection 註冊的服務,將來也會交給 Autofac 處理

      services.AddTransient<ITestServiceA, TestServiceA>();
      services.AddTransient<ITestServiceB, TestServiceB>();
      services.AddTransient<ITestServiceC, TestServiceC>();

      #endregion
    }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // builder.RegisterType<TestServiceA>().As<ITestServiceA>();
        // builder.RegisterType<TestServiceB>().As<ITestServiceB>();
        // builder.RegisterType<TestServiceC>().As<ITestServiceC>();
        builder.RegisterType<TestServiceD>().As<ITestServiceD>();
    }

Autofac 支援控制器屬性注入

控制器本身是一個類,它的例項其實是有 IControllerActivator 來建立的。

  1. 指定控制器的例項由容器來建立
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        
      #region 指定控制器的例項由容器來建立

      services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());

      #endregion
    }
  1. 註冊控制器的抽象和具體的關係
 	public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterType<TestServiceA>().As<ITestServiceA>().PropertiesAutowired();
        builder.RegisterType<TestServiceB>().As<ITestServiceB>();


        #region 註冊所有控制器的關係及控制器例項化所需要的元件

        var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
            .Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();

        builder.RegisterTypes(controllersTypesInAssembly)
            .PropertiesAutowired();

        #endregion
    }
  1. 在控制器內定義屬性
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public ITestServiceA TestServiceA { get; set; }
        public ITestServiceB TestServiceB { get; set; }
        
        [HttpGet]
        public IActionResult Get()
        {
            TestServiceA.Show();
            TestServiceB.Show();
            return Ok();
        }
    }
  1. 擴充套件:自己控制哪些屬性需要做依賴注入(預設是讓控制器中的屬性都依賴注入)
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class AutowaredAttribute : Attribute { }

    public class PropertySelector : IPropertySelector
    {
        public bool InjectProperty(PropertyInfo propertyInfo, object instance)
        {
            return propertyInfo.CustomAttributes.Any(ca => ca.AttributeType == typeof(AutowaredAttribute));
        }
    }
    public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterType<TestServiceA>().As<ITestServiceA>();
        builder.RegisterType<TestServiceB>().As<ITestServiceB>();

        #region 註冊所有控制器的關係及控制器例項化所需要的元件

        var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
            .Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();

        builder.RegisterTypes(controllersTypesInAssembly)
            .PropertiesAutowired(new PropertySelector());

        #endregion
    }
	[Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [Autowared]
        public ITestServiceA TestServiceA { get; set; }
        
        public ITestServiceB TestServiceB { get; set; }
        
        [HttpGet]
        public IActionResult Get()
        {
            TestServiceA.Show();
            TestServiceB.Show();
            return Ok();
        }
    }

Autofac 單例項多實現

 	public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterType<TestServiceA>().As<ITestServiceA>();
        builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
    }
  1. 如果多個實現同時註冊,則後註冊的實現就會覆蓋先註冊的實現,最後將返回最後一個註冊的實現。
  2. 如果多個實現同時註冊,可以通過一個 IEnumerable<例項> 來獲取到所有的實現。
        private readonly IEnumerable<ITestServiceA> _testServiceAs;

        public ValuesController(IEnumerable<ITestServiceA> testServiceAs)
        {
            _testServiceAs = testServiceAs;
        }
  1. 當多個實現同時註冊後,可以通過以下方式繼續註冊 例項 的所有實現。從而可以在控制器中直接使用具體實現類作為實現。
 	public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterType<TestServiceA>().As<ITestServiceA>();
        builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(type => type.IsAssignableTo<ITestServiceA>()));
    }
        private readonly TestServiceA _testServiceA;
        private readonly TestServiceUpdate _testServiceUpdate;

        public ValuesController(TestServiceA testServiceA,TestServiceUpdate testServiceUpdate)
        {
            _testServiceA = testServiceA;
            _testServiceUpdate = testServiceUpdate;
        }
  1. 擴充套件:Autofac 的註冊邏輯可以通過 Module 來拆分管理。
    public class AutofacModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // base.Load(builder);
            builder.RegisterType<TestServiceA>().As<ITestServiceA>();
            builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>();
            builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(type => type.IsAssignableTo<ITestServiceA>()));
        }
    }
 	public void ConfigureContainer(ContainerBuilder builder)
    {
 		// builder.RegisterModule(new AutofacModule());
        builder.RegisterModule<AutofacModule>();
    }

Autofac 支援 AOP

AOP 面向切面程式設計,通過預編譯方式和執行期動態代理實現程式功能的統一維護的一種技術。Autofac 的AOP是通過 Castle(也是一個容器)專案的名為 Autofac.Extras.DynamicProxy 核心部分實現的,顧名思義其實現方式為動態代理。

  1. 安裝 NuGet 程式包: Castle.Core 4.4.1Autofac.Extras.DynamicProxy 6.0.0

  2. 新建自定義 AOP 攔截器

 	public class CustomAutofacAop : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            {
                Console.WriteLine("方法執行前...");
            }

            invocation.Proceed();

            {
                Console.WriteLine("方法執行後...");
            }
        }
    }
  1. 在介面上標記需要使用的攔截器
[Intercept(typeof(CustomAutofacAop))]
public interface ITestServiceA
{
    void Show();
}
  1. 註冊自定義攔截器,並允許例項介面使用攔截器
    public void ConfigureContainer(ContainerBuilder builder)
    {
		//builder.RegisterType<CustomAutofacAop>();
        builder.RegisterType(typeof(CustomAutofacAop));
        builder.RegisterType<TestServiceA>().As<ITestServiceA>();
        builder.RegisterType<TestServiceUpdate>().As<ITestServiceA>().EnableInterfaceInterceptors();
    }
  1. 在控制器中呼叫例項,即可成功執行 AOP 攔截器
	[Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
		private readonly TestServiceA _testServiceA;
        private readonly TestServiceUpdate _testServiceUpdate;

        public ValuesController(TestServiceA testServiceA,TestServiceUpdate testServiceUpdate)
        {
            _testServiceA = testServiceA;
            _testServiceUpdate = testServiceUpdate;
        }

        public IActionResult Get()
        {
            _testServiceA.Show();
            _testServiceUpdate.Show();
            return Ok();
        }
    }