MyTetra Share
Делитесь знаниями!
Как разрешить размещенную службу в контроллере
Время создания: 08.03.2021 22:10
Раздел: Компьютер - C# - ASP
Запись: Kozlov-AE/Tetra/master/base/161523064042gp6pg14p/text.html на raw.githubusercontent.com

Try these two lines in startup.cs:

services.AddSingleton<TimedHealthCheckService>();

services.AddHostedService<TimedHealthCheckService>(provider => provider.GetService<TimedHealthCheckService>());


The first line above tells the service provider to create a singleton and give it to anyone who wants a TimedHealthCheckService, like your controller's constructor. However, the service provider is unaware that the singleton is actually an IHostedService and that you want it to call StartAsync().


The second line tells the service provider that you want to add a hosted service, so it'll call StartAsync() when the application starts running. AddHostedService accepts a Func<IServiceProvider,THostedService> callback. The callback we provide fetches the singleton TimedHealthCheckService from the service provider and returns it back to the service provider as an IHostedService. The service provider then calls its StartAsync() function.


And in your controller:

public HealthCheckController(ILogger<HealthCheckController> logger, TimedHealthCheckService hostedService)



 
MyTetra Share v.0.59
Яндекс индекс цитирования