How do you Resolve signalR v2.0 with StructureMap v2.6

时光毁灭记忆、已成空白 提交于 2020-01-02 09:32:18

问题


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?


回答1:


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...



来源:https://stackoverflow.com/questions/20705937/how-do-you-resolve-signalr-v2-0-with-structuremap-v2-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!