/// <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; }
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); }
public IActionResult MyGetTest2([FromServices] ISingletonService singletonService1, [FromServices] ISingletonService singletonService2, [FromServices] IScopeService scopeService1, [FromServices] IScopeService scopeService2, [FromServices] ITransientService transientService1, [FromServices] ITransientService transientService2) { Console.WriteLine($"构造函数 中获取的 singletonService1 的 hashCode :{ _singletonService.GetHashCode()}"); Console.WriteLine($"构造函数 中获取的 scopeService1 的 hashCode :{ _scopeService.GetHashCode()}"); Console.WriteLine($"构造函数 中获取的 transientService1 的 hashCode :{ _transientService.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 singletonService1 的 hashCode :{ singletonService1.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 singletonService2 的 hashCode :{ singletonService2.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 scopeService1 的 hashCode :{ scopeService1.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 scopeService2 的 hashCode :{ scopeService2.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 transientService1 的 hashCode :{ transientService1.GetHashCode()}"); Console.WriteLine($"HttpContext 中获取的 transientService2 的 hashCode :{ transientService2.GetHashCode()}"); //serviceProvider对象 Console.WriteLine($"HttpContext的ServiceProvider 的 hashCode :{ HttpContext.RequestServices.GetHashCode()}"); return(Content("MyGetTest")); }