经由Spring.Net SignalR依赖注入(SignalR dependency inject

2019-09-22 14:18发布

我试图通过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的默认实现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”中心无法解析。

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

Answer 1:

我认为这是很难得到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>();
}


文章来源: SignalR dependency injection via Spring.Net