public void M() { //记录M被调用次数 Console.WriteLine($"invoked {nameof(M)} for the {++_countm}. time"); //调用服务A _serviceA.A(); //调用服务B _serviceB.B(); }
private static void UsingScoped() { Console.WriteLine(nameof(UsingScoped)); ServiceProvider RegisterServices() { IServiceCollection services = new ServiceCollection(); services.AddSingleton <INumberService, NumberService>(); services.AddTransient <ControllerX>(); services.AddSingleton <IServiceB, ServiceB>(); services.AddScoped <IServiceA, ServiceA>(); services.AddTransient <IServiceC, ServiceC>(); return(services.BuildServiceProvider()); } using (ServiceProvider container = RegisterServices()) { using (IServiceScope scope1 = container.CreateScope()) { IServiceA a1 = scope1.ServiceProvider.GetRequiredService <IServiceA>(); a1.A(); IServiceA a2 = scope1.ServiceProvider.GetRequiredService <IServiceA>(); a2.A(); IServiceB b1 = scope1.ServiceProvider.GetRequiredService <IServiceB>(); b1.B(); IServiceB b2 = scope1.ServiceProvider.GetRequiredService <IServiceB>(); b2.B(); IServiceC c1 = scope1.ServiceProvider.GetRequiredService <IServiceC>(); c1.C(); IServiceC c2 = scope1.ServiceProvider.GetRequiredService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope 1"); using (IServiceScope scope2 = container.CreateScope()) { IServiceA a1 = scope2.ServiceProvider.GetRequiredService <IServiceA>(); a1.A(); IServiceA a2 = scope2.ServiceProvider.GetRequiredService <IServiceA>(); a2.A(); IServiceB b1 = scope2.ServiceProvider.GetRequiredService <IServiceB>(); b1.B(); IServiceB b2 = scope2.ServiceProvider.GetRequiredService <IServiceB>(); b2.B(); IServiceC c1 = scope2.ServiceProvider.GetRequiredService <IServiceC>(); c1.C(); IServiceC c2 = scope2.ServiceProvider.GetRequiredService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope 2"); } }
private static void UsingScoped() { Console.WriteLine(nameof(UsingScoped)); ServiceProvider RegisterServices() { var services = new ServiceCollection(); services.AddSingleton <INumberService, NumberService>(); services.AddScoped <IServiceA, ServiceA>(); services.AddSingleton <IServiceB, ServiceB>(); services.AddTransient <IServiceC, ServiceC>(); return(services.BuildServiceProvider()); } using (ServiceProvider container = RegisterServices()) { //调用ServiceProvider的CreateScope()方法创建一个作用域 //返回实现接口IServiceScope的作用域对象,在其中可以访问属于这个作用域的ServiceProvider using (IServiceScope scope1 = container.CreateScope()) { //因为IServiceA注册为Scoped,所以在同一作用域下返回的实例时一样的 IServiceA a1 = scope1.ServiceProvider.GetService <IServiceA>(); a1.A(); //A, 1 IServiceA a2 = scope1.ServiceProvider.GetService <IServiceA>(); a2.A(); //A, 1 //scope末尾不会释放IServiceB因为IServiceB是注册为单例,需要在作用域的末尾也是存活的,在程序结束时才会释放 IServiceB b1 = scope1.ServiceProvider.GetService <IServiceB>(); b1.B(); IServiceC c1 = scope1.ServiceProvider.GetService <IServiceC>(); c1.C(); IServiceC c2 = scope1.ServiceProvider.GetService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope1"); using (IServiceScope scope2 = container.CreateScope()) { IServiceA a3 = scope2.ServiceProvider.GetService <IServiceA>(); a3.A(); IServiceB b2 = scope2.ServiceProvider.GetService <IServiceB>(); b2.B(); IServiceC c3 = scope2.ServiceProvider.GetService <IServiceC>(); c3.C(); } Console.WriteLine("end of scope2"); } Console.WriteLine(); }
static void Main(string[] args) { using (ServiceProvider container = RegisterServices()) { for (int i = 0; i < 3; i++) { using (IServiceScope scope1 = container.CreateScope()) { IServiceA a1 = scope1.ServiceProvider.GetService <IServiceA>(); a1.A(); IServiceA a2 = scope1.ServiceProvider.GetService <IServiceA>(); a2.A(); IServiceB b1 = scope1.ServiceProvider.GetService <IServiceB>(); b1.B(); } Console.WriteLine($"End of scope {i}"); } } Console.Read(); }
public void M() { Console.WriteLine($"invoked {nameof(M)} for the { ++_countm}. time"); _serviceA.A(); _serviceB.B(); }
private static void UsingScoped() { Console.WriteLine(nameof(UsingScoped)); //局部方法 ServiceProvider RegisterServices() { var services = new ServiceCollection(); //注册单例服务 services.AddSingleton <INumberService, NumberService>(); //注册作用域服务 services.AddScoped <IServiceA, ServiceA>(); //注册单例服务 services.AddSingleton <IServiceB, ServiceB>(); //临时服务 services.AddTransient <IServiceC, ServiceC>(); //返回容器服务器 return(services.BuildServiceProvider()); } using (ServiceProvider container = RegisterServices()) { //创建自定义作用域scope1 using (IServiceScope scope1 = container.CreateScope()) { //IServiceA--ServiceA 在容器里是作用域服务 //a1和a2在作用域scop1里是一个对象 IServiceA a1 = scope1.ServiceProvider.GetService <IServiceA>(); a1.A(); //在作用域scope1里请求创建服务A的对象a2 IServiceA a2 = scope1.ServiceProvider.GetService <IServiceA>(); a2.A(); //此时a1和a2是一个对象 //IServiceB--ServiceB 在容器里是单例服务 //ServiceB在整个程序只有一个对象 IServiceB b1 = scope1.ServiceProvider.GetService <IServiceB>(); b1.B(); //IServiceC--ServiceC 在容器里是临时服务,C1和C2是两个对象 IServiceC c1 = scope1.ServiceProvider.GetService <IServiceC>(); c1.C(); IServiceC c2 = scope1.ServiceProvider.GetService <IServiceC>(); c2.C(); } Console.WriteLine("scope1结束"); //创建自定义作用域scope2 using (IServiceScope scope2 = container.CreateScope()) { //IServiceA--ServiceA 在容器里是作用域服务 //a3是与(a2和a1)不是一个对象 IServiceA a3 = scope2.ServiceProvider.GetService <IServiceA>(); a3.A(); //IServiceB--ServiceB 在容器里是单例服务 //ServiceB在整个程序只有一个对象,b2与b1都是一个对象 IServiceB b2 = scope2.ServiceProvider.GetService <IServiceB>(); b2.B(); //IServiceC--ServiceC 在容器里是临时服务,C1和C2及C3是三个不同的对象 IServiceC c3 = scope2.ServiceProvider.GetService <IServiceC>(); c3.C(); } Console.WriteLine("scope2结束"); Console.WriteLine(); } }