Back in .net core 2, I had created a hosted service with a custom property like:
public class MyService : BackgroundService
{
public bool IsRunning {get;set;}
...
That I could setup in startup.cs like:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHostedService,HostedServices.MyService>();
...
And then I could reference it elsewhere in a razor page like:
public class IndexModel : PageModel
{
private readonly IHostedService _mySrv;
public IndexModel(IHostedService mySrv) => _mySrv = mySrv;
[BindProperty]
public bool IsRunning { get; set; }
public void OnGet() => IsRunning = ((HostedServices.MyService)_mySrv).IsRunning;
}
Now that I've upgraded to .net core 3, my startup has changed to:
services.AddHostedService<HostedServices.MyService>();
But my DI reference in IndexModel doesn't get me my MyService anymore, it gives me an object of type GenericWebHostService instead, that I can't figure out how to get my custom MyService from. Changing 'IHostedService' to 'MyService' in IndexModel doesn't work either, i get a 'Unable to resolve service' error.
How do I get an instance of MyService back from dependency injection?
StartAsync
andStopAsync
on the service if you just add it as a singleton. Also this answer seems to contradict your claims. – Saraband