Asp.net mvc 4 通过Spring.Net的信号器依赖注入

Asp.net mvc 4 通过Spring.Net的信号器依赖注入,asp.net-mvc-4,spring.net,signalr,Asp.net Mvc 4,Spring.net,Signalr,我正试图通过Spring.NET注入依赖项 首先,我创建了一个自定义DependencyResolver: public class SignalRSpringNetDependencyResolver : DefaultDependencyResolver { private IApplicationContext _context; public SignalRSpringNetDependencyResolver(IApplicationContext context)

我正试图通过Spring.NET注入依赖项

首先,我创建了一个自定义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总是试图使用我分配的解析程序解析它自己的依赖关系,我得到以下错误:

无法解析“myhub”集线器


我只需要解析器知道其他依赖项(例如我的存储库),并保留SignalR服务的默认实现

我认为很难让Spring.Net与Signal一起工作

对于当前版本(Spring.Net 1.3.2),很难支持异步编程。Spring.Net会话管理不能很好地处理
任务
类型

我最终通过两个步骤注入了依赖项:

1-在WebActivator PostStart上注册所需类型:

GlobalHost.DependencyResolver.Register(
    typeof(IUserService), 
    () => (UserService)ctx.GetObject("UserService"))
2-在我的集线器构造器中拾取它们:

public MyHub() 
{
    _userService =
        DependencyResolver.Current.GetService<IUserService>();
}
publicmyhub()
{
_用户服务=
DependencyResolver.Current.GetService();
}

我认为很难让Spring.Net与Signal一起工作

对于当前版本(Spring.Net 1.3.2),很难支持异步编程。Spring.Net会话管理不能很好地处理
任务
类型

我最终通过两个步骤注入了依赖项:

1-在WebActivator PostStart上注册所需类型:

GlobalHost.DependencyResolver.Register(
    typeof(IUserService), 
    () => (UserService)ctx.GetObject("UserService"))
2-在我的集线器构造器中拾取它们:

public MyHub() 
{
    _userService =
        DependencyResolver.Current.GetService<IUserService>();
}
publicmyhub()
{
_用户服务=
DependencyResolver.Current.GetService();
}
您是否见过信号器的
依赖解析程序
?这有一些关于SignalR+mvc 3+SignalR.Ninject的信息。你看过SignalR的
DependencyResolver
吗?这有一些关于SignalR+mvc 3+SignalR.Ninject的信息。