How to configure Autofac and SignalR in a MVC 5 application

后端 未结 3 1667
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 05:17

I am trying to configure an MVC 5 application to use SignalR 2.2.x and inject a service into my NotificationsHub. We are using Autofac for MVC but I am not sure

3条回答
  •  無奈伤痛
    2020-12-21 05:49

    You should tell SignalR explicitly to use the AutofacDependencyResolver when you are mapping it. I assume that you know that in your Startup class you have to call app.MapSignalR();

    When you are mapping it, you should tell it to use the custom dependency resolver (the AutofacDependencyResolver).

    Here's how I do it:

    var resolver = new AutofacDependencyResolver(container);
    
    app.MapSignalR(new HubConfiguration
    {
        Resolver = resolver
    });
    

    This way, you are telling SignalR directly which dependency resolver to use.

    I have a GitHub repo for SignalR Dependency Injection, but it's not configured to use MVC. Still, I think it will give you a hint in how to create your configuration.

    Note: If you are using the OWIN Middleware, be sure not to use the GlobalHost static property AT ALL since it will have massive inconsistencies.

    A common error in OWIN integration is use of the GlobalHost. In OWIN you create the configuration from scratch. You should not reference GlobalHost anywhere when using the OWIN integration.

    Again, check the repo I gave you to see how to do this.

    Hope this helps:) Best of luck!

提交回复
热议问题