1.配置文件的讀取
利用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;
}
//讀取Option1節點
this.Configuration["Option1"];
//讀取subsection下的suboption1節點
this.Configuration["subsection:suboption1"];
//讀取subsection下的Read節點
string[] _SqlConnectionStringRead = this._iConfiguration.GetSection("subsection").GetSection("Read").GetChildren().Select(s => s.Value).ToArray();
2.自帶IOC容器
1.基本使用
NuGet安裝引用Microsoft.Extensions.DependencyInjection;
實例化容器
注冊服務
創建一個System.IServiceProvider提供程序
創建實例
using Microsoft.Extensions.DependencyInjection;
//實例化一個服務容器
IServiceCollection container = new ServiceCollection();
//添加一個瞬時類型到容器
container.AddTransient<Interface,class>();
//創建一個服務provider
IServiceProvider provider = container.BuildServiceProvider();
//從provider中獲取接口實例
_Interface interface = provider.GetService<_Interface>()
2.服務類型
container.AddTransient()
最終創建的服務為瞬時生命周期,並且每次創建都是一個全新的實例
_Interface interface = provider.GetService<_Interface>();
_Interface interface2 = provider.GetService<_Interface>();
//interface 和 interface2持有的引用不相等,說明不是一個
bool bResult = object.ReferenceEquals(interface, interface2);
//b is false
container.AddSingleton()
最終創建的服務為單例的:全容器都是這一個不變的實例
_Interface interface = provider.GetService<_Interface>();
_Interface interface2 = provider.GetService<_Interface>();
//interface 和 interface2持有的引用相等,說明是一個
bool bResult = object.ReferenceEquals(interface, interface2);
//b is True
container.AddScoped
最終創建的服務為請求單例,一個請求代表一個作用域,在同一請求下構建的相同的服務實例只有一個
//創建一個Scope作用域,那么由這個域創建的實例是獨立的(相較Scope1)
IServiceScope Scope = provider.CreateScope();
//創建一個Scope1作用域,那么由這個域創建的實例是獨立的
IServiceScope Scope1 = provider.CreateScope();
_Interface interface = Scope.ServiceProvider.GetService<_Interface>();
_Interface interface2 = Scope1.ServiceProvider.GetService<_Interface>();
//interface 和 interface2持有的引用不相等,因為不是一個作用域
bool bResult = object.ReferenceEquals(interface, interface2);
//b is false
3.自帶IOC容器(IServiceCollection)基礎
1.生命周期
瞬時,即時構造,即時銷毀
services.AddTransient<ITestServiceA, TestServiceA>();
單例,永遠只構造一次
services.AddSingleton<ITestServiceB, TestServiceB>();
作用域單例,一次請求只構造一個
services.AddScoped<ITestServiceC, TestServiceC>();
2.實例解析
1.為了方便理解,就不搞一些貓啊狗之類的,當然也比較簡陋。我們一共定義了A、B、C、D、E這幾個類,代碼也比較簡單,類與類關聯關系如下,
A類構造
B類構造需要A類
C類構造需要B類
D類構造
E類構造需要C類
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}被構造。。。");
}
2.我們首先把他們添加到IOC容器中,然后再使用IOC創建他們的實例
//瞬時
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是瞬時,上一次已經被銷毀,所以是一個全新的A),然后構造B類為單例
3.開始創建C,在構造時需要B類,因為B類全局單例已經存在,那就會直接構造C為作用域單例
4.直接創建瞬時生命周期D類
5.開始創建瞬時生命周期E類,構造時需要C類,因為C類為作用域單例,那么就會直接構造E類為瞬時
第一次請求結論
1.因為瞬時所以輸出A被構造
2.輸出A被構造,B被構造,此時B全局單例
3.C被創建時需要B,但是B是全局單例,所以不會被構造,所以只輸出C被構造
4.輸出D被構造
5.C為作用域單例,B為全局單例,所以只輸出E被構造
第二次請求解析
1.開始構造A類,因為A是瞬時生命周期那么第二次請求就會重新被構造,然后銷毀
2.在構造B類時因為在第一次請求時已經構造為單例,所以不再被構造,直接使用
3.開始重新構造C,因為C在第二次請求為新的作用域,在構造時需要B類,因為B類全局單例,那直接構造C為當前新的作用域單例
4.直接構造瞬時生命周期D類
5.開始構造E類,構造式需要C類,因為C類為作用域單例,那么就會直接構造E類為瞬時
第二次請求結論
1.輸出A被構造
2.輸出C被構造
3.輸出D被構造
4.輸出E被構造
3.手動創建實例
有人會說,如果不想通過構造函數全部自動注入,我們可以使用服務Procider自己在使用時創建對應自己需要的服務實例
1.引入命名空間using Microsoft.Extensions.DependencyInjection
;
2.首先注入 IServiceProvider serviceProvider
,利用serviceProcider.GetService<ITestServiceA>();
生成需要的實例
4.AutoFac使用
1.Autofac容器替換自帶容器
1.下載
Autofac.Extensions.DependencyInjection
和Autofac
包
2.在
Program
網站啟動時
替換默認IOC工廠實例為Autofac
,UseServiceProviderFactory(new AutofacServiceProviderFactory())
3.在Startup類中添加方法
//返回值為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>();
}
2.批量注冊模塊
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();
}
}
3.利用AutoFac實現AOP
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}執行完成了");
}
}