[Asp.Net Core]Autaofa的生命周期

Posted 厦门德仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Asp.Net Core]Autaofa的生命周期相关的知识,希望对你有一定的参考价值。

Autaofa的生命周期

            #region Autaofa的生命周期 
            
                生命周期范围
                //ContainerBuilder containerBuilder = new ContainerBuilder();
                //IContainer container = containerBuilder.Build();
                //using (var scope = container.BeginLifetimeScope())
                //

                //
            
            
                 //瞬时生命周期 ---每一期获取对象都是一个全新的实例(默认的生命)
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerDependency();
                    IContainer container = containerBuilder.Build();
                    ITestServiceA testServiceA = container.Resolve<ITestServiceA>();//获取服务
                    ITestServiceA testServiceA1 = container.Resolve<ITestServiceA>();//获取服务
                    Console.WriteLine(object.ReferenceEquals(testServiceA, testServiceA1));
                

                 //单例生命周期---在整个进程中,对象永远都是同一个实例(SingleInstance)
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().SingleInstance();
                    IContainer container = containerBuilder.Build();
                    ITestServiceA testServiceA = container.Resolve<ITestServiceA>();//获取服务
                    ITestServiceA testServiceA1 = container.Resolve<ITestServiceA>();//获取服务
                    Console.WriteLine(object.ReferenceEquals(testServiceA, testServiceA1));
                

                 //每个生命周期范围一个实例--同一个生命周期范围内是同一个实例,不同的生命周期范围,实例不一样;  (InstancePerLifetimeScope)
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerLifetimeScope();
                    IContainer container = containerBuilder.Build();

                    ITestServiceA testServiceA15 = null;
                    ITestServiceA testServiceA16 = null;
                    using (var scope1 = container.BeginLifetimeScope()) //生命周期范围
                    
                        ITestServiceA testServiceA11 = scope1.Resolve<ITestServiceA>();
                        ITestServiceA testServiceA12 = scope1.Resolve<ITestServiceA>();
                        Console.WriteLine(object.ReferenceEquals(testServiceA11, testServiceA12));
                        testServiceA15 = testServiceA12;
                    

                    using (var scope1 = container.BeginLifetimeScope())
                    
                        ITestServiceA testServiceA13 = scope1.Resolve<ITestServiceA>();
                        ITestServiceA testServiceA14 = scope1.Resolve<ITestServiceA>();
                        Console.WriteLine(object.ReferenceEquals(testServiceA13, testServiceA14));
                        testServiceA16 = testServiceA14;
                    
                    Console.WriteLine(object.ReferenceEquals(testServiceA15, testServiceA16));
                

                 //每个【匹配生命周期范围一个实例】(InstancePerMatchingLifetimeScope(名称))
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerMatchingLifetimeScope("Dezai");

                    IContainer container = containerBuilder.Build();

                    ITestServiceA testServiceA15 = null;
                    ITestServiceA testServiceA16 = null;
                    using (var scope1 = container.BeginLifetimeScope("Dezai"))
                    
                        ITestServiceA testServiceA11 = scope1.Resolve<ITestServiceA>();
                        using (var scope2 = scope1.BeginLifetimeScope())
                        
                            ITestServiceA testServiceA12 = scope2.Resolve<ITestServiceA>();
                            Console.WriteLine(object.ReferenceEquals(testServiceA11, testServiceA12));
                        
                        testServiceA15 = testServiceA11;
                    

                    using (var scope1 = container.BeginLifetimeScope("Dezai"))
                    
                        ITestServiceA testServiceA13 = scope1.Resolve<ITestServiceA>();
                        using (var scope2 = scope1.BeginLifetimeScope())
                        
                            ITestServiceA testServiceA14 = scope2.Resolve<ITestServiceA>();
                            Console.WriteLine(object.ReferenceEquals(testServiceA13, testServiceA14));
                        

                        testServiceA16 = testServiceA13;
                    
                    Console.WriteLine(object.ReferenceEquals(testServiceA15, testServiceA16));
                

                
                    //每个请求一个实例(InstancePerRequest)
                    ContainerBuilder containerBuilder = new ContainerBuilder();
                    containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>().InstancePerRequest();
                    IContainer container = containerBuilder.Build();
                    ITestServiceA testServiceA11 = container.Resolve<ITestServiceA>();
                    ITestServiceA testServiceA12 = container.Resolve<ITestServiceA>();
                    Console.WriteLine(object.ReferenceEquals(testServiceA11, testServiceA12));

                

            
            #endregion

以上是关于[Asp.Net Core]Autaofa的生命周期的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core 应用程序生命周期

Asp.Net Core 服务生命周期

ASP.NET Core 和 JWT 令牌生命周期

ASP.NET Core SignalR 中的客户端生命周期事件

[Asp.Net Core]内置容器生命周期

[Asp.Net Core]内置容器生命周期