Simple Injector registration problem in SignalR
Asked Answered
R

1

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?

Radium answered 29/7, 2019 at 15:24 Comment(8)
Version of Aspnet? Core, Framework? Version of SignalR?Police
@JamieRees .Net Framework 4.8, ASP.NET MVC 5.2.7.0, Microsoft.AspNet.SignalR 2.4.1.0.Radium
You have to register the implementation with the container in order for it to be injected when resolving dependentsEmissive
@Emissive 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...Radium
@Emissive Any help please?Radium
Youneed to start by reviewing the documentation that matches the version you are using. learn.microsoft.com/en-us/aspnet/signalr/overview/…Emissive
I had already reviewed and tried some suggestion. But get "The given type IHubContext<DemoHub> is not a concrete type. Please use one of the other overloads to register this type." error.Radium
@JamieRees Any help please?Radium
V
5

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;
    }
}
Volume answered 29/7, 2019 at 23:46 Comment(4)
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<THub,T> Interface in ASP.NET MVC?.Radium
In this scene, I cannot use ITypedClient and only pass IHubContext<DemoHub> to the Controller. I tried your answer for this usage as container.Register<IHubContext<JiraHub>> and return GlobalHost.ConnectionManager.GetHubContext<JiraHub, IHubContext>() but encounter the following error: "Cannot implicitly convert type 'Microsoft.AspNet.SignalR.IHubContext<Microsoft.AspNet.SignalR.IHubContext>' to 'Microsoft.AspNet.SignalR.IHubContext<DemoHub>'. An explicit conversion exists (are you missing a cast?)"Radium
@hexadecimal I've updated my answer. Pay attention that you should use IHubContext<> with client interface and not with hub classVolume
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 :)Radium

© 2022 - 2024 — McMap. All rights reserved.