2012-09-19 77 views
2

我试图通过Spring.NET注入依赖关系。通过Spring.Net的SignalR依赖注入

首先,我创建了一个定制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; 
    } 

请注意,我逃避接口和抽象类,这样我会得到SignalR的默认实现从基础DefaultDependencyResolver

这里我分配使用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); 

    } 

但是,SignalR是总是试图解决它使用的解析器自己的依赖我分配的,我得到以下错误:

'myhub' hub could not be resolved.

我只需要解析器要注意其他依赖(我的仓库为例),并保持SignalR的默认实现服务。

+0

你见过SignalR的'DependencyResolver'的[Ninject实现](https://github.com/SignalR/SignalR.Ninject)吗?这[博客文章](http://lcdev.dk/2012/02/14/using-signalr-ninject-with-asp-net-mvc3-and-the-ninject-mvc3-nuget-package/)有一些信息用于SignalR + mvc 3 + Signalr.Ninject。 – Marijn

回答

0

我觉得这是很难得Spring.Net与SignalR

当前版本(1.3.2 Spring.Net)工作很难支持异步编程。 Spring.Net会话管理在Task<T>类型中播放不好。

我结束了我的注入在依赖两个步骤:

1上WebActivator开始后注册所需的类型:

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

2-接他们在我中心的构造函数:

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