How do I get a reference to an IHostedService via Dependency Injection in ASP.NET Core?

后端 未结 3 913
粉色の甜心
粉色の甜心 2021-02-05 11:04

Details

I have attempted to create a background processing structure using the recommended IHostedService interface in ASP.NET 2.1. I register the service

3条回答
  •  遇见更好的自我
    2021-02-05 12:05

    Current workaround from mentioned git page:

    services.AddSingleton();
    services.AddSingleton(p => p.GetService());
    

    This creates your service as hosted (runs and stops at host's start and shutdown), as well as gets injected as depedency wherever you require it to be.

提交回复
热议问题