2

I set DI in my Controller as shown below and tied to register IHubContext as it seen on

Controller:

public class DemoController : Controller
{
    private IHubContext<DemoHub> context;

    public DemoController(IHubContext<DemoHub> context)
    {
        this.context = context;
    }
}


Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();



    container.Register<IHubContext, IHubContext>(Lifestyle.Scoped);

    // or 

    container.Register<IHubContext>(Lifestyle.Scoped);

    // code omitted
}

But when I debug my app, encounter "System.ArgumentException: 'The given type IHubContext is not a concrete type. Please use one of the other overloads to register this type. Parameter name: TImplementation'" error. So, how can I register IHubContext properly?

  • 1
    Version of Aspnet? Core, Framework? Version of SignalR? – Jamie Rees Jul 29 '19 at 15:26
  • @JamieRees .Net Framework 4.8, ASP.NET MVC 5.2.7.0, Microsoft.AspNet.SignalR 2.4.1.0. –  Jul 29 '19 at 15:42
  • 1
    You have to register the implementation with the container in order for it to be injected when resolving dependents – Nkosi Jul 29 '19 at 15:49
  • @Nkosi How can I do this? Could you pls post it as answer? Sorry, but I have really no idea about that and I have tried many thing to fix the problem. Thanks... –  Jul 29 '19 at 15:54
  • @Nkosi Any help please? –  Jul 29 '19 at 16:00
  • 1
    Youneed to start by reviewing the documentation that matches the version you are using. https://learn.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc – Nkosi Jul 29 '19 at 16:18
  • I had already reviewed and tried some suggestion. But get "*The given type IHubContext is not a concrete type. Please use one of the other overloads to register this type.*" error. –  Jul 29 '19 at 16:57
  • @JamieRees Any help please? –  Jul 30 '19 at 09:20

1 Answers1

4

Since ASP.NET MVC doesn't have built in dependency injection for SignalR hub context you have to obtain a context instance using GlobalHost.ConnectionManager. With this you can register a dependency with your container that creates IHubContext instance. Considering you have typed hub

public class DemoHub : Hub<ITypedClient>
{
}

and interface

public interface ITypedClient
{
    void Test();
}

register dependency as the following

container.Register<IHubContext<ITypedClient>>(() =>
{
    return GlobalHost.ConnectionManager.GetHubContext<DemoHub, ITypedClient>();
}, Lifestyle.Scoped);

And the controller should look like

public class DemoController : Controller
{
    private IHubContext<ITypedClient> context;

    public DemoController(IHubContext<ITypedClient> context)
    {
        this.context = context;
    }
}
Alexander
  • 9,104
  • 1
  • 17
  • 41
  • Thanks a lot for your helps. Actually I tried to use ITypedClient at first, but as I use .NET MVC (rather than .NET Core) I cannot use Microsoft.AspNetCore.SignalR and have to use Microsoft.AspNet.SignalR dll. For this reason I cannot pass ITypedClient to the Controller as I mentioned on [SignalR : How to use IHubContext Interface in ASP.NET MVC?](https://stackoverflow.com/questions/57250585/signalr-how-to-use-ihubcontextthub-t-interface-in-asp-net-mvc). –  Jul 30 '19 at 06:33
  • In this scene, I cannot use ITypedClient and only pass `IHubContext` to the Controller. I tried your answer for this usage as `container.Register>` and `return GlobalHost.ConnectionManager.GetHubContext()` but encounter the following error: "*Cannot implicitly convert type 'Microsoft.AspNet.SignalR.IHubContext' to 'Microsoft.AspNet.SignalR.IHubContext'. An explicit conversion exists (are you missing a cast?)*" –  Jul 30 '19 at 06:34
  • 1
    @hexadecimal I've updated my answer. Pay attention that you should use `IHubContext<>` with client interface and **not** with hub class – Alexander Jul 30 '19 at 10:18
  • You rock!.. Thanks a lot for your helps and wonderful explanations... The problem was solved now and I can proceed to complete my SignalR implementation :) –  Jul 30 '19 at 12:33