StructureMap and SignalR - IMessageBus, no default instance defined

回眸只為那壹抹淺笑 提交于 2019-12-24 12:55:27

问题


I used nuget to update my project to signalr 2.2 and structure map 2.6.4.
Now when my program attempts to use SignalR, structure map is throwing this error:

    StructureMap.StructureMapException was unhandled by user code
      HResult=-2146232832
      Message=StructureMap Exception Code:  202
    No Default Instance defined for 
PluginFamily Microsoft.AspNet.SignalR.Messaging.IMessageBus, Microsoft.AspNet.SignalR.Core, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
      Source=StructureMap

My code hasn't change and I don't believe I'm requiring IMessageBus anywhere, so I'm not sure why structure map is now doing this. I setup a new project with a simplified structuremap 2.6.4/signalr 2.2 and never need to seed IMessageBus, so it's something about my implementation, but I'm not sure what's changed from the upgrade.

Does anyone have an idea to point me to?

Thanks! Scott


回答1:


This was solved by first looking to resolve in the base class:

public override object GetService(Type serviceType)
{
        if (serviceType == null)
            return null;

var service = base.GetService(serviceType);
        if (service != null) return service;

return container.TryGetInstance(serviceType);

}



回答2:


What solved this for me was making a change to the GetService method as well. Here is my full StrucutreMapSignalRDependencyResolver for reference:

public class StructureMapSignalRDependencyResolver : DefaultDependencyResolver
{
    private readonly IContainer _container;

    public StructureMapSignalRDependencyResolver(IContainer container)
    {
        _container = container;
    }

    public override object GetService(Type serviceType)
    {
        return _container.TryGetInstance(serviceType) ?? base.GetService(serviceType);
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        var objects = _container.GetAllInstances(serviceType).Cast<object>();
        return objects.Concat(base.GetServices(serviceType));
    }
}


来源:https://stackoverflow.com/questions/28448652/structuremap-and-signalr-imessagebus-no-default-instance-defined

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