.Net Core依賴注入

一、配置文件的讀取

利用Startup類中的configuration讀取appsettings.json中的配置

{
"Logging": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"option1": "value1_from_json",
"option2": 2,
"subsection": {
  "suboption1": "subvalue1_from_json",
  "Read": [
    "Data Source=.; Database=Customers_New1; User ID=sa; Password=Passw0rd; MultipleActiveResultSets=True",
    "Data Source=ElevenPC; Database=Customers_New2; User ID=sa; Password=Passw0rd; MultipleActiveResultSets=True",
    "Data Source=.; Database=Customers_New3; User ID=sa; Password=Passw0rd; MultipleActiveResultSets=True"
  ]
},
"wizards": [
  {
    "Name": "Gandalf",
    "Age": "1000"
  },
  {
    "Name": "Harry",
    "Age": "17"
  }
],
"AllowedHosts": "*"
}
 public IConfiguration Configuration { get; }
 public Startup(IConfiguration configuration)
 {
     Configuration = configuration;
 }
 
 this.Configuration["Option1"]
 this.Configuration["subsection:suboption1"]
 string[] _SqlConnectionStringRead = this._iConfiguration.GetSection("subsection").GetSection("Read").GetChildren().Select(s => s.Value).ToArray();

二、自帶IOC容器

1.基本使用
  1. NuGet安裝引用Microsoft.Extensions.DependencyInjection;
  2. 實例化容器
  3. 註冊服務
  4. 創建一個System.IServiceProvider提供程式
  5. 創建實例
 using Microsoft.Extensions.DependencyInjection;
 IServiceCollection container = new ServiceCollection();
 container.AddTransient<Interface,class>();
 IServiceProvider provider = container.BuildServiceProvider();
 _Interface  interface = provider.GetService<_Interface>()
2.服務類型

1.container.AddTransient為瞬時生命周期,每次創建都是一個全新的實例

IServiceCollection container = new ServiceCollection();
container.AddTransient<Interface,class>();
IServiceProvider provider = container.BuildServiceProvider();
_Interface  interface = provider.GetService<_Interface>();
_Interface  interface2 = provider.GetService<_Interface>();
bool bResult = object.ReferenceEquals(interface, interface2);
//b is false 
  

2.container.AddSingleton單例:全容器都是一個

IServiceCollection container = new ServiceCollection();
container.AddSingleton<Interface,class>();
IServiceProvider provider = container.BuildServiceProvider();
_Interface  interface = provider.GetService<_Interface>();
_Interface  interface2 = provider.GetService<_Interface>();
bool bResult = object.ReferenceEquals(interface, interface2);
 //b is True 
    

3.container.AddScoped請求單例,一個請求代表一個作用域

IServiceCollection container = new ServiceCollection();
container.AddScoped<Interface,class>();
IServiceProvider provider = container.BuildServiceProvider();

//創建一個Scope作用域,那麼由這個域創建的實例是獨立的(相較Scope1)
IServiceScope Scope = provider.CreateScope();
//創建一個Scope1作用域,那麼由這個域創建的實例是獨立的
IServiceScope Scope1 = provider.CreateScope();

_Interface  interface =Scope.ServiceProvider.GetService<_Interface>();
_Interface  interface2 = Scope1.ServiceProvider.GetService<_Interface>();
bool bResult = object.ReferenceEquals(interface, interface2);
 //b is false 
    

三、自帶IOC容器(IServiceCollection)基礎

1.生命周期

1.瞬時,即時構造,即時銷毀

services.AddTransient<ITestServiceA, TestServiceA>();

2.單例,永遠只構造一次

 services.AddSingleton<ITestServiceB, TestServiceB>();

3.作用域單例,一次請求只構造一個

  services.AddScoped<ITestServiceC, TestServiceC>(); 
2.實例解析
public TestServiceA()
{
    Console.WriteLine($"{this.GetType().Name}被構造。。。");
}

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

public TestServiceC(ITestServiceB iTestServiceB)
{
 Console.WriteLine($"{this.GetType().Name}被構造。。。");
}

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

public TestServiceE(ITestServiceC serviceC)
{
   Console.WriteLine($"{this.GetType().Name}被構造。。。");
}

//1.A類構造
//2.B類構造需要A類
//3.C類構造需要B類
//4.D類構造
//5.E類構造需要C類

//瞬時
services.AddTransient<ITestServiceA, TestServiceA>();
//單例
services.AddSingleton<ITestServiceB, TestServiceB>();
//作用域單例
services.AddScoped<ITestServiceC, TestServiceC>();
//瞬時
services.AddTransient<ITestServiceD, TestServiceD>();
//瞬時
services.AddTransient<ITestServiceE, TestServiceE>();

第一次請求解析

1.開始構造A類,因為是瞬時生命周期那麼第一次請求就會被構造,然後銷毀

2.在構造B類時需要A類,那麼會首先構造A類(因為A類瞬時上一次已經被銷毀),然後構造B類為單例

3.開始構造C,在構造時需要B類,因為B類全局單例,那就會直接構造C為作用域單例

4.直接構造瞬時D類

5.開始構造E類,構造式需要C類,因為C類為作用域單例,那麼就會直接構造E類為瞬時

第一次請求結論

  1. 輸出A
  2. 輸出A,B
  3. 輸出C
  4. 輸出D
  5. 輸出E

第二次請求解析

1.開始構造A類,因為是瞬時生命周期那麼第二次請求就會重新被構造,然後銷毀

2.在構造B類時因為在第一次請求時已經構造為單例,所以不再被構造

3.開始重新構造C,因為C在第二次請求為新的作用域,在構造時需要B類,因為B類全局單例,那就會直接構造C為作用域單例

4.直接構造瞬時D類

5.開始構造E類,構造式需要C類,因為C類為作用域單例,那麼就會直接構造E類為瞬時

第二次請求結論

1.輸出A
2.輸出C
3.輸出D
4.輸出E

如果不想通過構造全部自動注入,能自定義注入

1.using Microsoft.Extensions.DependencyInjection;

2.首先注入 IServiceProvider serviceProvider,利用serviceProcider.GetService();生成需要的實例

四、利用AutoFac容器替換自帶容器

1.下載Autofac.Extensions.DependencyInjection和Autofac包

2.在Program網站啟動時替換默認IOC工廠實例為Autofac,UseServiceProviderFactory(new AutofacServiceProviderFactory())

3.在Startup類中添加方法public void ConfigureContainer(ContainerBuilder services){services.RegisterType().As().SingleInstance();}

 //返回值為IServiceProvider
 public IServiceProvider ConfigureContainer(ContainerBuilder services)
 {
  services.Configure<CookiePolicyOptions>(options =>
  {
      options.CheckConsentNeeded = context => true;
      options.MinimumSameSitePolicy = SameSiteMode.None;
  });
  services.AddSession();
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  //services.RegisterType<TestServiceE>().As<ITestServiceE>().SingleInstance();
   services.RegisterModule<CustomMoudle>();

}
1.批量註冊模組

public class CustomAutofacModule : Module
{
    //重新load 添加註冊服務
    //可以實現實例控制
    //介面約束
   protected override void Load(ContainerBuilder containerBuilder)
   {
       containerBuilder.Register(c => new CustomAutofacAop());
      
      //設置為屬性注入
       containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().SingleInstance().PropertiesAutowired();
       
       containerBuilder.RegisterType<TestServiceC>().As<ITestServiceC>();
       containerBuilder.RegisterType<TestServiceB>().As<ITestServiceB>();
       containerBuilder.RegisterType<TestServiceD>().As<ITestServiceD>();

       containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();
   }
}

五、利用AutoFac實現AOP

1.基本使用

1.NuGet安裝引用Autofac.Extras.DynamicProxy

2.引用using Autofac.Extras.DynamicProxy;

3.創建類庫繼承自IInterceptor實現介面

4.自定義的Autofac的AOP擴展

 //註冊,模組
public class CustomAutofacModule : Module
{
     //重新load 添加註冊服務
     //可以實現實例控制
     //介面約束
    protected override void Load(ContainerBuilder containerBuilder)
    {
        //註冊AOP
        containerBuilder.Register(c => new CustomAutofacAop());
        //開啟aop擴展
        containerBuilder.RegisterType<A>().As<IA>().EnableInterfaceInterceptors();
    }
}

public class CustomAutofacAop : IInterceptor
{
   public void Intercept(IInvocation invocation)
   {
       Console.WriteLine($"invocation.Methond={invocation.Method}");
       Console.WriteLine($"invocation.Arguments={string.Join(",", invocation.Arguments)}");

       invocation.Proceed(); //繼續執行

       Console.WriteLine($"方法{invocation.Method}執行完成了");
   }
}