本文介绍了通过Spring.Net SignalR依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Spring.NET注入的依赖关系。

I'm trying to inject dependencies via Spring.NET.

首先,我创建了一个定制DependencyResolver:

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;
    }

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

在这里我使用分配的WebActivator解析:

and here I assigned the resolver 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);

    }

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

However, SignalR is always trying to resolve it's own dependencies using the Resolver i assigned and i get the following error:

myhub中心未能得到解决。

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

I only need the resolver to be aware of other dependencies(my Repository for example) and keep the default implementation of SignalR services.

推荐答案

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

I think it's hard to get Spring.Net working with SignalR

当前版本(1.3.2 Spring.Net),这是很难支持异步编程。 Spring.Net会话管理不符合任务&LT发挥出色; T&GT; 类型

for the current version (Spring.Net 1.3.2) it's difficult to support asynchronous programming. Spring.Net session management doesn't play well with Task<T> types.

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

I ended up injecting my dependencies in 2 steps:

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

1- registering the required type on WebActivator PostStart:

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

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

2- picking them up in my Hub constructor:

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

这篇关于通过Spring.Net SignalR依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:17