Question

In Application_Start of Global.asax i have the following

 ObjectFactory.Initialize(cfg => {
     cfg.For<IDependencyResolver>().Singleton().Add<StructureMapDependencyResolver>    ();
 });

My Interface for the Hub is

public interface IDashboardHub
{
    void Initialize();
}

and my hub is as follows:

public class DashboardHub : Hub, IDashboardHub
{
    private readonly ICpeAccountService _accountService;

    public DashboardHub(ICpeAccountService service)
    {
        _accountService = service;
    }

    [Authorize]
    public void Initialize()
    {
        Clients.All.UpdateStatus("Hello World!!!");
    }
}

If I remove the injected constructor and the Resolver then I get the "Hello World" signal and the JavaScript display the value. If I just remove the resolver then signalR no longer finds a parameterless constructor and the Initialize methods does not get called.

If I include the StructureMap dependency resolver (which is working and injecting around 40 other classes right now) then i get the following exception message

StructureMap configuration failures: Error:  104
Source:  Registry:  StructureMap.Configuration.DSL.Registry, StructureMap,
Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
Type Instance '87da3c00-4deb-4334-b189-021d445d95ec' 

(Configured Instance of App.DependencyResolution.StructureMapDependencyResolver, 
    App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)     
    Cannot be plugged into type Microsoft.AspNet.SignalR.IDependencyResolver, 
    Microsoft.AspNet.SignalR.Core, Version=2.0.0.0, Culture=neutral,     
    PublicKeyToken=31bf3856ad364e35

Also if I try to just resolve this all in startup.cs, like this:

public void Configuration(IAppBuilder app)
{
    ObjectFactory.Initialize(cfg =>
    {
            cfg.For<IDependencyResolver>()
               .Singleton()
               .Add<StructureMapDependencyResolver>();
        });

    app.MapSignalR();
}

I also get the same error.

Has anyone been able to resolve this issue?

Was it helpful?

Solution

The easiest way is to use a HubActivator

All you need in startup,cs is

public void Configuration(IAppBuilder app)
{
    app.MapSignalR();
}

Create a Activator for your hubs

public class HubActivator : IHubActivator
{
    private readonly IContainer container;

    public HubActivator(IContainer container)
    {
        this.container = container;
    }

    public IHub Create(HubDescriptor descriptor)
    {
        return (IHub)container.GetInstance(descriptor.HubType);
    }
}

Make sure that you register this activator in app_start

IContainer container = StructureMap.Container();

// Register a Hub Activator for SignalR
GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => new HubActivator(container));

and then remove any SignalRDependencyResolver code as its not needed...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top