Accessing ASP.NET Core DI Container From Static Factory Class

后端 未结 5 1614
闹比i
闹比i 2021-01-30 16:57

I\'ve created an ASP.NET Core MVC/WebApi site that has a RabbitMQ subscriber based off James Still\'s blog article Real-World PubSub Messaging with RabbitMQ.

In his arti

5条回答
  •  时光取名叫无心
    2021-01-30 17:26

    You can get the service reference in Configure:

    app.UseMvc();
    var myServiceRef = app.ApplicationServices.GetService();
    

    and then ie pass it to an init function or set a static member on a class

    of course dependency injection would be a better solution as explained in the other answers ...

提交回复
热议问题