集成温莎城堡与SignalR - 我应该如何处理呢? [英] Integrating Castle Windsor with SignalR - how should I approach this?

查看:131
本文介绍了集成温莎城堡与SignalR - 我应该如何处理呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用SignalR,一旦一切都配置它的伟大工程。然而,几乎所有我在使用温莎城堡工作,所以这将是伟大的应用程序能够一起使用它们。我想这样做的原因是,这样我可以使用一个持久连接内部城堡依赖/服务。

I am getting started with SignalR, and it works great once everything is configured. However, almost all the applications that I work on use Castle Windsor, so it would be great to be able to use them together. The reason that I want to do this is so that I can use Castle dependencies/services inside of a persistent connection.

我在源$ C ​​$ C周围挖,它看起来像我既可以使用基于城堡(即是城实施的IDependencyResolver)取代DependencyResolver,我也可以DependencyResolver的使用改为城堡。

I dug around in the source code, and it looks like I could either replace DependencyResolver with a Castle based one (i.e., Castle implementing IDependencyResolver), or I could change the usage of DependencyResolver to Castle.

其中哪一个是一个更好的主意吗?有没有我可以用它来城堡和SignalR结合另一种方法?

Which one of these is a better idea? Is there another approach that I could use to combine Castle and SignalR?

谢谢,
埃里克

Thanks, Erick

推荐答案

我设置的第一个选择了我们自己的DependencyResolver

I went with the first option of setting our own DependencyResolver

AspNetHost.SetResolver(new SignalResolver(_container));

我可以提供SignalResolver如果需要的话,但离开了提高可读性现在。

I can provide SignalResolver if desired but leaving out for readability for now.

另外要注意的是,集线器必须有一个空的构造函数,因此我们的城堡集装箱通过属性注入,例如

Another important note is that the hubs must have an empty constructor so our castle container injects through properties, e.g.

public class NotificationHub : Hub, INotificationHub
    { 

public INotificationService NotificationService { get; set; }

和要求解析器

public class SignalResolver : DefaultDependencyResolver
    {
        private readonly IWindsorContainer _container;

        public SignalResolver(IWindsorContainer container) 
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            _container = container;
        }

        public override object GetService(Type serviceType) 
        {
            return TryGet(serviceType) ?? base.GetService(serviceType);
        }

        public override IEnumerable<object> GetServices(Type serviceType)
        {
            return TryGetAll(serviceType).Concat(base.GetServices(serviceType));
        }

        private object TryGet(Type serviceType)
        {
            try
            {
                return _container.Resolve(serviceType);
            }
            catch (Exception)
            {
                return null;
            }
        }

        private IEnumerable<object> TryGetAll(Type serviceType)
        {
            try
            {
                var array = _container.ResolveAll(serviceType);
                return array.Cast<object>().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

这篇关于集成温莎城堡与SignalR - 我应该如何处理呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆