Exemplo n.º 1
0
        public bool GetService([FromServices] IMySingletonService singletonService,
                               [FromServices] IMySingletonService singletonService1,
                               [FromServices] IMyScopedService scopedService,
                               [FromServices] IMyScopedService scopedService1,
                               [FromServices] IMyTransientService transientService,
                               [FromServices] IMyTransientService transientService1
                               )
        {
            Console.WriteLine("请求开始GetService();");

            Console.WriteLine($"{nameof(IMySingletonService)}.singletonService:{singletonService.GetHashCode()}");
            Console.WriteLine($"{nameof(IMySingletonService)}.singletonService1:{singletonService1.GetHashCode()}");


            Console.WriteLine($"{nameof(IMyScopedService)}.scopedService:{scopedService.GetHashCode()}");
            Console.WriteLine($"{nameof(IMyScopedService)}.scopedService1:{scopedService1.GetHashCode()}");


            Console.WriteLine($"{nameof(IMyTransientService)}.transientService:{transientService.GetHashCode()}");
            Console.WriteLine($"{nameof(IMyTransientService)}.transientService1:{transientService1.GetHashCode()}");

            Console.WriteLine("请求结束");

            return(true);
        }
 public int GetService(
     [FromServices] IMySingletonService singletonService1,
     [FromServices] IMySingletonService singletonService2
     )
 {
     Console.WriteLine(singletonService1.GetHashCode());
     Console.WriteLine(singletonService2.GetHashCode());
     return 1;
 }
Exemplo n.º 3
0
        public void GetService2([FromServices] IMySingletonService singletonService,
                                [FromServices] IMyScopedService scopedService,
                                [FromServices] IMyTransientService transientService
                                )
        {
            Console.WriteLine("请求开始GetService2()");

            Console.WriteLine($"{nameof(IMySingletonService)}.singletonService:{singletonService.GetHashCode()}");
            Console.WriteLine($"{nameof(IMyScopedService)}.scopedService:{scopedService.GetHashCode()}");
            Console.WriteLine($"{nameof(IMyTransientService)}.transientService:{transientService.GetHashCode()}");

            //GetTransientService();

            Console.WriteLine("请求结束");
        }
        public bool Get()
        {
            Console.WriteLine($"【瞬时实例】_myTransientService1:{_myTransientService1.GetHashCode()}");
            Console.WriteLine($"【瞬时实例】_myTransientService2:{_myTransientService2.GetHashCode()}");

            Console.WriteLine($"【域内单例】_myScopedService1:{_myScopedService1.GetHashCode()}");
            Console.WriteLine($"【域内单例】_myScopedService2:{_myScopedService2.GetHashCode()}");

            Console.WriteLine($"【全局单例】_mySingletonService1:{_mySingletonService1.GetHashCode()}");
            Console.WriteLine($"【全局单例】_mySingletonService2:{_mySingletonService2.GetHashCode()}");

            Console.WriteLine($"========请求结束=======");

            return(true);
        }
Exemplo n.º 5
0
        public bool Get()
        {
            Console.WriteLine($"单例1:{_mySingletonService1.GetHashCode()}");
            Console.WriteLine($"单例2:{_mySingletonService2.GetHashCode()}");

            Console.WriteLine($"瞬时1:{_myTransientService1.GetHashCode()}");
            Console.WriteLine($"瞬时2:{_myTransientService2.GetHashCode()}");

            Console.WriteLine($"域内单例1:{_myScopedService1.GetType()}:{_myScopedService1.GetHashCode()}");
            Console.WriteLine($"域内单例2:{_myScopedService2.GetType()}:{_myScopedService2.GetHashCode()}");

            Console.WriteLine($"========请求结束=======");

            return(true);
        }
        public ActionResult GetServices([FromServices] IMySingletonService singletonService1,
                                        [FromServices] IMySingletonService singletonService2,
                                        [FromServices] IMyTransientService transientService1,
                                        [FromServices] IMyTransientService transientService2,
                                        [FromServices] IMyScopedService scopedService1,
                                        [FromServices] IMyScopedService scopedService2)
        {
            Console.WriteLine($"singleton1: {singletonService1.GetHashCode()}");
            Console.WriteLine($"singleton2: {singletonService2.GetHashCode()}");
            Console.WriteLine($"transient1: {transientService1.GetHashCode()}");
            Console.WriteLine($"transient2: {transientService2.GetHashCode()}");
            Console.WriteLine($"scoped1: {scopedService1.GetHashCode()}");
            Console.WriteLine($"scoped2: {scopedService2.GetHashCode()}");

            return(Content("OK"));
        }
Exemplo n.º 7
0
        public int Get([FromServices] IMySingletonService singleton1,
                       [FromServices] IMySingletonService singleton2,
                       [FromServices] IMyTransientService myTransient1,
                       [FromServices] IMyTransientService myTransient2,
                       [FromServices] IMyScopedService myScoped1,
                       [FromServices] IMyScopedService myScoped2)
        {
            Console.WriteLine($"singleton1: {singleton1.GetHashCode()}");
            Console.WriteLine($"singleton2: {singleton2.GetHashCode()}");
            Console.WriteLine($"Transient: {myTransient1.GetHashCode()}");
            Console.WriteLine($"Transient: {myTransient2.GetHashCode()}");
            Console.WriteLine($"Scoped: {myScoped1.GetHashCode()}");
            Console.WriteLine($"Scoped: {myScoped2.GetHashCode()}");

            Console.WriteLine($"=====请求结束====");
            return(1);
        }
Exemplo n.º 8
0
        public int GetServices([FromServices] IMySingletonService singletonService1,
                               [FromServices] IMySingletonService singletonService2,
                               [FromServices] IMyTransientService transientService1,
                               [FromServices] IMyTransientService transientService2,
                               [FromServices] IMyScopedService scopedService1,
                               [FromServices] IMyScopedService scopedService2)
        {
            Console.WriteLine($" singletonService1 的 HashCode={singletonService1.GetHashCode()}");
            Console.WriteLine($" singletonService2 的 HashCode={singletonService2.GetHashCode()}");

            Console.WriteLine($" transientService1 的 HashCode={transientService1.GetHashCode()}");
            Console.WriteLine($" transientService2 的 HashCode={transientService2.GetHashCode()}");

            Console.WriteLine($" scopedService1 的 HashCode={scopedService1.GetHashCode()}");
            Console.WriteLine($" scopedService2 的 HashCode={scopedService2.GetHashCode()}");
            return(1);
        }
Exemplo n.º 9
0
        public int GetServices([FromServices] IMySingletonService mySingleton1,
                               [FromServices] IMySingletonService mySingleton2,
                               [FromServices] IMyScopedService myScopedService1,
                               [FromServices] IMyScopedService myScopedService2,
                               [FromServices] IMYTransientService mYTransientService1,
                               [FromServices] IMYTransientService mYTransientService2)
        {
            Console.WriteLine($"mySingleton1:{mySingleton1.GetHashCode()}");
            Console.WriteLine($"mySingleton2:{mySingleton2.GetHashCode()}");
            Console.WriteLine($"myScopedService1:{myScopedService1.GetHashCode()}");
            Console.WriteLine($"myScopedService2:{myScopedService2.GetHashCode()}");
            Console.WriteLine($"mYTransientService1:{mYTransientService1.GetHashCode()}");
            Console.WriteLine($"mYTransientService2:{mYTransientService2.GetHashCode()}");
            Console.WriteLine("=========请求结束============");

            return(1);
        }
Exemplo n.º 10
0
        public bool Get([FromServices] IMyTransientService myTransientService1, [FromServices] IMyTransientService myTransientService2,
                        [FromServices] IMyScopedService myScopedService1, [FromServices] IMyScopedService myScopedService2,
                        [FromServices] IMySingletonService mySingletonService1, [FromServices] IMySingletonService mySingletonService2)
        {
            Console.WriteLine($"【瞬时实例】myTransientService1:{myTransientService1.GetHashCode()}");
            Console.WriteLine($"【瞬时实例】myTransientService2:{myTransientService2.GetHashCode()}");

            Console.WriteLine($"【域内单例】myScopedService1:{myScopedService1.GetHashCode()}");
            Console.WriteLine($"【域内单例】myScopedService2:{myScopedService2.GetHashCode()}");

            Console.WriteLine($"【全局单例】mySingletonService1:{mySingletonService1.GetHashCode()}");
            Console.WriteLine($"【全局单例】mySingletonService2:{mySingletonService2.GetHashCode()}");

            Console.WriteLine($"========请求结束=======");

            return(true);
        }
 public string GetService(
     [FromServices] IMySingletonService singleton1,
     [FromServices] IMySingletonService singleton2,
     [FromServices] IMyTransientService transient1,
     [FromServices] IMyTransientService transient2,
     [FromServices] IMyScopedService scoped1,
     [FromServices] IMyScopedService scoped2)
 {
     Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
     Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");
     Console.WriteLine($"transient1:{transient1.GetHashCode()}");
     Console.WriteLine($"transient2:{transient2.GetHashCode()}");
     Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
     Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");
     Console.WriteLine($"========请求结束========");
     return("GetService");
 }
Exemplo n.º 12
0
        public int GetService([FromServices] IMySingletonService singleton1,
                              [FromServices] IMySingletonService singleton2,
                              [FromServices] IMyTransientService transient1,
                              [FromServices] IMyTransientService transient2,
                              [FromServices] IMyScopedService scope1,
                              [FromServices] IMyScopedService scope2)
        {
            Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
            Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");

            Console.WriteLine($"transient1:{transient1.GetHashCode()}");
            Console.WriteLine($"transient2:{transient2.GetHashCode()}");

            Console.WriteLine($"scope1:{scope1.GetHashCode()}");
            Console.WriteLine($"scope2:{scope2.GetHashCode()}");

            return(1);
        }
Exemplo n.º 13
0
        public int GetService([FromServices] IMySingletonService mySingletonService1,
                              [FromServices] IMySingletonService mySingletonService2,
                              [FromServices] IMyTransientService myTransientService1,
                              [FromServices] IMyTransientService myTransientService2,
                              [FromServices] IMyScopeService myScopeService1,
                              [FromServices] IMyScopeService myScopeService2)
        {
            Console.WriteLine($"mySingleton1:{mySingletonService1.GetHashCode()}");
            Console.WriteLine($"mySingleton2:{mySingletonService2.GetHashCode()}");
            Console.WriteLine($"myTransient1:{myTransientService1.GetHashCode()}");
            Console.WriteLine($"myTransient2:{myTransientService2.GetHashCode()}");
            Console.WriteLine($"myScoped1:{myScopeService1.GetHashCode()}");
            Console.WriteLine($"myScoped2:{myScopeService2.GetHashCode()}");



            return(0);
        }
Exemplo n.º 14
0
        public int GetService([FromServices] IMySingletonService singletonService1,
                              [FromServices] IMySingletonService singletonService2,
                              [FromServices] IMyScopeService scopeService1,
                              [FromServices] IMyScopeService scopeService2,
                              [FromServices] IMyTransientService transientService1,
                              [FromServices] IMyTransientService transientService2)
        {
            Console.WriteLine($"{nameof(singletonService1)}:{singletonService1.GetHashCode()}");
            Console.WriteLine($"{nameof(singletonService2)}:{singletonService2.GetHashCode()}");

            Console.WriteLine($"{nameof(scopeService1)}:{scopeService1.GetHashCode()}");
            Console.WriteLine($"{nameof(scopeService2)}:{scopeService2.GetHashCode()}");

            Console.WriteLine($"{nameof(transientService1)}:{transientService1.GetHashCode()}");
            Console.WriteLine($"{nameof(transientService2)}:{transientService2.GetHashCode()}");

            Console.WriteLine("=========请求结束========");
            return(1);
        }
        public int GetService(
            [FromServices] IMySingletonService singletonService1,
            [FromServices] IMySingletonService singletonService2,
            [FromServices] IMyScopedService scopedService1,
            [FromServices] IMyScopedService scopedService2,
            [FromServices] IMyTransientService transientService1,
            [FromServices] IMyTransientService transientService2)
        {
            Console.WriteLine($"singleton1:{singletonService1.GetHashCode()}");
            Console.WriteLine($"singleton2:{singletonService2.GetHashCode()}");
            Console.WriteLine($"scoped1:{scopedService1.GetHashCode()}");
            Console.WriteLine($"scoped2:{scopedService2.GetHashCode()}");
            Console.WriteLine($"transient1:{transientService1.GetHashCode()}");
            Console.WriteLine($"transient2:{transientService2.GetHashCode()}");
            Console.WriteLine($"========请求结束========");

            //第一次打印:
            //singleton1: 3129430
            //singleton2: 3129430
            //scoped1: 37901460
            //scoped2: 37901460
            //transient1: 4436986
            //transient2: 10553853
            //========请求结束========

            //第二次打印:
            //singleton1: 3129430
            //singleton2: 3129430
            //scoped1: 39086322
            //scoped2: 39086322
            //transient1: 36181605
            //transient2: 28068188
            //========请求结束========

            // 结论:
            // 1.singleton:在应用程序创建至销毁整个生命周期中都是同一个服务
            // 2.scoped:在同一请求中为同一服务,不用请求不同服务
            // 3.transient:每获取一次,创建一个新的服务


            return(0);
        }
Exemplo n.º 16
0
        public int GetService([FromServices] IMySingletonService singleton1, // 通过控制器参数获取服务
                              [FromServices] IMySingletonService singleton2,
                              [FromServices] IMyTransientService transient1,
                              [FromServices] IMyTransientService transient2,
                              [FromServices] IMyScopedService scoped1,
                              [FromServices] IMyScopedService scoped2)
        {
            Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
            Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");

            Console.WriteLine($"transient1:{transient1.GetHashCode()}");
            Console.WriteLine($"transient2:{transient2.GetHashCode()}");

            Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
            Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");
            Console.WriteLine($"scoped3:{scoped3.GetHashCode()}");

            Console.WriteLine($"========请求结束=======");
            return(1);
        }
Exemplo n.º 17
0
        public String GetService([FromServices] IMySingletonService singleton1,
                                 [FromServices] IMySingletonService singleton2,
                                 [FromServices] IMyScopedService scoped1,
                                 [FromServices] IMyScopedService scoped2,
                                 [FromServices] IMyTransientService transient1,
                                 [FromServices] IMyTransientService transient2)
        {
            Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
            Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");

            Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
            Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");

            Console.WriteLine($"transient1:{transient1.GetHashCode()}");
            Console.WriteLine($"transient2:{transient2.GetHashCode()}");

            Console.WriteLine("请求结束");

            return("Get servies from IoC container");
        }
Exemplo n.º 18
0
        public int Index([FromServices] IMySingletonService _iMySingletonService1, // [FromServices]:用于从容器中获取对象
                         [FromServices] IMySingletonService _iMySingletonService2,
                         [FromServices] IMyTransientService _iMyTransientService1,
                         [FromServices] IMyTransientService _iMyTransientService2,
                         [FromServices] IMyScopedService _iMyScopedService1,
                         [FromServices] IMyScopedService _iMyScopedService2)
        {
            Console.WriteLine($"单例模式的 HashCode 是相同的:");
            Console.WriteLine($"_iMySingletonService1: {_iMySingletonService1.GetHashCode()}");
            Console.WriteLine($"_iMySingletonService2: {_iMySingletonService2.GetHashCode()}");
            Console.WriteLine($"");

            Console.WriteLine($"瞬时模式的 HashCode 是不同的:");
            Console.WriteLine($"_iMyTransientService1: {_iMyTransientService1.GetHashCode()}");
            Console.WriteLine($"_iMyTransientService2: {_iMyTransientService2.GetHashCode()}");
            Console.WriteLine($"");

            Console.WriteLine($"作用域模式的 HashCode 在单次访问时相同的,再次访问是不同的。即每次请求生成新的对象:");
            Console.WriteLine($"_iMyScopedService1: {_iMyScopedService1.GetHashCode()}");
            Console.WriteLine($"_iMyScopedService2: {_iMyScopedService2.GetHashCode()}");
            Console.WriteLine($"");

            return(1);
        }