When I include a structuremap dependency resolver in my global.asax for signalR any calls to client functions no longer arrive at the browser although I can see them if in the logging pipeline.
Here's an extract of my global.asax:
var container = ObjectFactory.Container;
// Now configure SignalR, MVC, ASP.Net and SharpRepository
GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
GlobalHost.HubPipeline.AddModule(new LoggingPipelineModule());
// TODO: Work out why this breaks the connection between the server and the client.
GlobalHost.DependencyResolver =
ObjectFactory.GetInstance<IDependencyResolver>();
RouteTable.Routes.MapHubs();
DependencyResolver.SetResolver(
new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
new StructureMapDependencyResolver(container);
RepositoryDependencyResolver.SetDependencyResolver(
new SharpRepository.Ioc.StructureMap.StructureMapDependencyResolver(container));
Here the implementation of the structuremap resolver:
public class StructureMapSignalRDependencyResolver
: DefaultDependencyResolver
{
private readonly IContainer _container;
public StructureMapSignalRDependencyResolver(
IContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
_container = container;
}
public override object GetService(Type serviceType)
{
object result = null;
try
{
result =
!serviceType.IsAbstract &&
!serviceType.IsInterface &&
serviceType.IsClass
? _container.GetInstance(serviceType)
: (_container.TryGetInstance(serviceType)
?? base.GetService(serviceType));
}
catch (Exception ex)
{
Tracing.Error(
"[StructureMapSignalRDependencyResolver]",
Tracing.SerializeException(ex));
}
if (result == null)
{
Tracing.Information(
"[StructureMapSignalRDependencyResolver]",
"Could retrieve object of type {0}",serviceType.ToString());
}
return result;
}
public override IEnumerable<object> GetServices(
Type serviceType)
{
IEnumerable<object> result = null;
try
{
result = _container.GetAllInstances(serviceType)
.Cast<object>().Concat(
base.GetServices(serviceType));
}
catch (Exception ex)
{
Tracing.Error(
"[StructureMapSignalRDependencyResolver]",
Tracing.SerializeException(ex));
}
if (result == null)
{
Tracing.Information(
"[StructureMapSignalRDependencyResolver]",
"Could retrieve object of type {0}", serviceType.ToString());
}
return result;
}
public override void Register(Type serviceType,
Func<object> activator)
{
Tracing.Information(
"[StructureMapSignalRDependencyResolver]",
"Registering object of type {0}",
serviceType.ToString());
base.Register(serviceType, activator);
}
public override void Register(Type serviceType,
IEnumerable<Func<object>> activators)
{
Tracing.Information(
"[StructureMapSignalRDependencyResolver]",
"Registering object of type {0}",
serviceType.ToString());
base.Register(serviceType, activators);
}
}
Here is the implementation of StructureMap Registry (there are many more which include the application assemblies).
[RegistryOrder(Order = 6)]
public class SignalRRegistry : Registry
{
public SignalRRegistry()
{
For<IDependencyResolver>().Singleton()
.Use<StructureMapSignalRDependencyResolver>();
For<IHubConnectionContext>().Singleton()
.Use(GlobalHost.ConnectionManager
.GetHubContext<BOSSHub>().Clients);
}
//public void Configure()
//{
// GlobalHost.DependencyResolver =
// ObjectFactory.GetInstance<IDependencyResolver>();
// RouteTable.Routes.MapHubs();
//}
}
I've rolled back the Hub class so that it no longer has an dependencies; the output from WhatDoIHave seems to have some entries for Microsoft.AspNet.SignalR but without any concrete relationships.
I wondering if I missed a step in my registry ? I get no exceptions anywhere; it just stops working, if I comment out the GlobalHost.DependencyResolver line from global.asax all is well.
I hope someone could share with me a DI implementation for signalR that is working for them.
Many Thanks
UPDATE: Thanks for sorting out the bad formatting for me - appreciate that.
UPDATE: I've put together a small test project which shows the issue. I was surprised if I'm honest I was able to reproduce it as the application that I'm working on it pretty big and complex with lot's of StructureMap stuff going on. I've uploaded into GitHub : https://github.com/johnk27stars/SignalRTest.git - Thanks to anyone who could spare a minute to take a look.