Exemple #1
0
 /// <summary>
 /// 两种注入方式:构造函数注入....FromServices注入....
 /// </summary>
 /// <param name="logger"></param>
 /// <param name="_singletonService"></param>
 /// <param name="_scopedService"></param>
 /// <param name="_transientService"></param>
 /// <param name="_singletonService2"></param>
 /// <param name="_scopedService2"></param>
 /// <param name="_transientService2"></param>
 public WeatherForecastController(ILogger <WeatherForecastController> logger, ISingletonService _singletonService, IScopedService _scopedService, ITransientService _transientService, ISingletonService _singletonService2, IScopedService _scopedService2, ITransientService _transientService2)
 {
     Console.WriteLine($"singleton:{_singletonService.GetHashCode()}");
     Console.WriteLine($"singleton:{_singletonService2.GetHashCode()}");
     Console.WriteLine($"scoped:{_scopedService.GetHashCode()}");
     Console.WriteLine($"scoped:{_scopedService2.GetHashCode()}");
     Console.WriteLine($"transient:{_transientService.GetHashCode()}");
     Console.WriteLine($"transient:{_transientService2.GetHashCode()}");
     _logger = logger;
 }
Exemple #2
0
 public void GetService([FromServices] ISingletonService singleton1,
                        [FromServices] ISingletonService singleton2,
                        [FromServices] IScopedService scoped1,
                        [FromServices] IScopedService scoped2,
                        [FromServices] ITransientService transient1,
                        [FromServices] ITransientService transient2
                        )
 {
     System.Console.WriteLine($"singleton1:{singleton1.GetHashCode()}");
     System.Console.WriteLine($"singleton2:{singleton2.GetHashCode()}");
     System.Console.WriteLine($"scoped1:{scoped1.GetHashCode()}");
     System.Console.WriteLine($"scoped2:{scoped2.GetHashCode()}");
     System.Console.WriteLine($"transient1:{transient1.GetHashCode()}");
     System.Console.WriteLine($"transient2:{transient2.GetHashCode()}");
 }
        public IActionResult Index([FromServices] ISingletonService singletonService2)
        {
            var viewModel = new HomeViewModel
            {
                Scoped     = scopedService.GetHashCode(),
                Scoped2    = scopedService2.GetHashCode(),
                Singleton  = singletonService.GetHashCode(),
                Transient  = transientService.GetHashCode(),
                Transient2 = transientService2.GetHashCode(),
            };

            //Use the singleton service which is injected via methos injection
            var singletonObjectHashCode = singletonService2.GetHashCode();

            return(View(viewModel));
        }
        public int GetService([FromServices] ISingletonService singleton1,
                              [FromServices] ISingletonService singleton2,
                              [FromServices] IScopedService scoped1,
                              [FromServices] IScopedService scoped2,
                              [FromServices] ITransientService transient1,
                              [FromServices] ITransientService 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(1);
        }
Exemple #5
0
 public void ScopedService()
 {
     _logger.Log("Comando1::ScopedService " + _scopeSrv.GetHashCode());
     _scopeSrv.Ejecutar();
 }