How to inject a reference to a specific IHostedService implementation?

后端 未结 6 2143
半阙折子戏
半阙折子戏 2020-12-03 10:43

My web app has a background service that listens to a service bus. Based on the docs, it looks like the built-in way to run a background service is to implement IHoste

6条回答
  •  天涯浪人
    2020-12-03 11:16

    None of the complexity in the other answers is needed as of .net core 3.1. If you don't need to get a concrete reference to your class from another class, simply call:

    services.AddHostedService();
    

    If you must have a concrete reference, do the following:

    services.AddSingleton();
    

提交回复
热议问题