Enabling SignalR Dependencies Through Spring.Net

I am trying to inject dependencies through Spring.NET.

First I created a custom DependencyResolver:

public class SignalRSpringNetDependencyResolver : DefaultDependencyResolver { private IApplicationContext _context; public SignalRSpringNetDependencyResolver(IApplicationContext context) { _context = context; } /// <summary> /// Gets the application context. /// </summary> /// <value>The application context.</value> public IApplicationContext ApplicationContext { get { if (_context == null || _context.Name != ApplicationContextName) { if (string.IsNullOrEmpty(ApplicationContextName)) { _context = ContextRegistry.GetContext(); } else { _context = ContextRegistry.GetContext(ApplicationContextName); } } return _context; } } /// <summary> /// Gets or sets the name of the application context. /// </summary> /// <remarks> /// Defaults to using the root (default) Application Context. /// </remarks> /// <value>The name of the application context.</value> public static string ApplicationContextName { get; set; } /// <summary> /// Resolves singly registered services that support arbitrary object creation. /// </summary> /// <param name="serviceType">The type of the requested service or object.</param> /// <returns>The requested service or object.</returns> public override object GetService(Type serviceType) { System.Diagnostics.Debug.WriteLine(serviceType.FullName); if (serviceType != null && !serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass) { var services = ApplicationContext.GetObjectsOfType(serviceType).GetEnumerator(); services.MoveNext(); try { return services.Value; } catch (InvalidOperationException) { return null; } } else { return base.GetService(serviceType); } } /// <summary> /// Resolves multiply registered services. /// </summary> /// <param name="serviceType">The type of the requested services.</param> /// <returns>The requested services.</returns> public override IEnumerable<object> GetServices(Type serviceType) { var services = ApplicationContext.GetObjectsOfType(serviceType).Cast<object>(); services.Concat(base.GetServices(serviceType)); return services; } 

Note that I avoid interfaces and abstract classes in order to get standard SignalR implementations from the DefaultDependencyResolver base

and here I assigned a converter using WebActivator:

  public static void PostStart() { // Inject Dependencies to SignalR, should be always come before ASP.NET MVC configuration var dependecyResolver = new SignalRSpringNetDependencyResolver(ContextRegistry.GetContext()); GlobalHost.DependencyResolver = dependecyResolver; RouteTable.Routes.MapHubs(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 

However, SignalR always tries to resolve its own dependencies using the assigned Resolver i, and I get the following error:

'myhub' hub cannot be resolved.

I only need a resolver to know about other dependencies (for example, in my repository) and to support the standard implementation of SignalR services.

+2
source share
1 answer

I find it hard to get Spring.Net working with SignalR

for the current version (Spring.Net 1.3.2) it is difficult to support asynchronous programming. Spring.Net session management does not work well with Task<T> types.

As a result, I introduced my dependencies in 2 stages:

1- registration of the required type in WebActivator PostStart:

 GlobalHost.DependencyResolver.Register( typeof(IUserService), () => (UserService)ctx.GetObject("UserService")) 

2- selecting them in my hub constructor:

 public MyHub() { _userService = DependencyResolver.Current.GetService<IUserService>(); } 
0
source

All Articles