2

我正在尝试通过 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;
    }

请注意,我转义了接口和抽象类,以便从基础 DefaultDependencyResolver 获取 SignalR 的默认实现

在这里,我使用 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' 集线器无法解决。

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

4

1 回答 1

0

我认为很难让 Spring.Net 与 SignalR 一起使用

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

我最终分两步注入了我的依赖项:

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

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

2- 在我的 Hub 构造函数中获取它们:

public MyHub() 
{
    _userService =
        DependencyResolver.Current.GetService<IUserService>();
}
于 2012-09-20T12:53:57.613 回答