StructureMap and SignalR - IMessageBus, no default

2019-08-03 08:50发布

问题:

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));
    }
}