8

我开始使用 SignalR,一旦配置好一切,它就可以很好地工作。但是,我所开发的几乎所有应用程序都使用 Castle Windsor,因此能够将它们一起使用会很棒。我想这样做的原因是我可以在持久连接中使用 Castle 依赖项/服务。

我仔细研究了源代码,看起来我可以用基于 Castle 的替代 DependencyResolver(即,实现 IDependencyResolver 的 Castle),或者我可以将 DependencyResolver 的用法更改为 Castle。

其中哪一个是更好的主意?我可以使用另一种方法来结合 Castle 和 SignalR 吗?

谢谢,埃里克

4

3 回答 3

6

2016 年 8 月更新

根据评论,我不再使用下面的方法,但现在使用 GlobalHost.DependencyResolver

所以在 Global.asax.cs 我初始化的东西

public static void Init(IWindsorContainer container)
{
    var conn = configurationManager.ConnectionStrings["SRSQL"].ConnectionString;
    GlobalHost.DependencyResolver.Register(typeof(IHubActivator), 
                                      () => new SignalHubActivator(container));
    GlobalHost.DependencyResolver.Register(typeof(ILoggingService), 
                                      container.Resolve<ILoggingService>);
    //etc or you could just pass your existing container to the resolver
    GlobalHost.DependencyResolver.UseSqlServer(conn);    
}

然后在集线器

private ILoggingService LoggingService{ get; set; }

    public NotificationHub()
    {
        LoggingService = GlobalHost.DependencyResolver.Resolve<ILoggingService>();
    }

并且为了完整性

public class SignalHubActivator: IHubActivator
{
    private readonly IWindsorContainer _container;

    public SignalHubActivator(IWindsorContainer container)
    {
        _container = container;
    }


    public IHub Create(HubDescriptor descriptor)
    {
                var result=  _container.Resolve(descriptor.HubType) as IHub;

        if (result is Hub)
        {
            _container.Release(result);
        }

    return result;
    }

}

2012年的旧答案

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

AspNetHost.SetResolver(new SignalResolver(_container));

如果需要,我可以提供 SignalResolver,但现在不考虑可读性。

另一个重要的注意事项是集线器必须有一个空的构造函数,以便我们的城堡容器通过属性注入,例如

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;
            }
        }
    }
于 2012-05-11T10:31:24.577 回答
3

这就是我最终做的事情。首先,我跟随 Windsor wiki 获取我的 ASP.NET MVC3 设置。我的 Global.asax.cs:

private static IWindsorContainer _container;

    protected void Application_Start()
    {
        BootstrapContainer();
        RegisterRoutes(RouteTable.Routes);
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
    }

    protected void Application_End()
    {
        _container.Dispose();
    }

    private static void BootstrapContainer()
    {
        _container = new WindsorContainer().Install(FromAssembly.This());
        RouteTable.Routes.MapHubs(new CastleWindsorDependencyResolver(_container));
        var controllerFactory = new WindsorControllerFactory(_container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }
    ...

CastleWindsorDependencyResolver 来自这里

复制:

public class CastleWindsorDependencyResolver : DefaultDependencyResolver
{
    private readonly IWindsorContainer _container;

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

        _container = container;

        // perform the lazy registrations
        foreach (var c in _lazyRegistrations)
            _container.Register(c);

        _lazyRegistrations.Clear();
    }

    public override object GetService(Type serviceType)
    {
        if (_container.Kernel.HasComponent(serviceType))
            return _container.Resolve(serviceType);
        return base.GetService(serviceType);
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        IEnumerable<object> objects;
        if (_container.Kernel.HasComponent(serviceType))
            objects = _container.ResolveAll(serviceType).Cast<object>();
        else
            objects = new object[] { };

        var originalContainerServices = base.GetServices(serviceType);
        if (originalContainerServices != null)
            return objects.Concat(originalContainerServices);

        return objects;
    }

    public override void Register(Type serviceType, Func<object> activator)
    {
        if (_container != null)
            // cannot unregister components in windsor, so we use a trick
            _container.Register(Component.For(serviceType).UsingFactoryMethod<object>(activator, true).OverridesExistingRegistration());
        else
            // lazy registration for when the container is up
            _lazyRegistrations.Add(Component.For(serviceType).UsingFactoryMethod<object>(activator));

        // register the factory method in the default container too
        //base.Register(serviceType, activator);
    }

    // a form of laxy initialization is actually needed because the DefaultDependencyResolver starts initializing itself immediately
    // while we now want to store everything inside CastleWindsor, so the actual registration step have to be postponed until the
    // container is available
    private List<ComponentRegistration<object>> _lazyRegistrations = new List<ComponentRegistration<object>>();
}

public static class WindsorTrickyExtensions
{
    /// <summary>
    /// Overrideses the existing registration:
    /// to overide an existiong component registration you need to do two things:
    /// 1- give it a name.
    /// 2- set it as default.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="componentRegistration">The component registration.</param>
    /// <returns></returns>
    public static ComponentRegistration<T> OverridesExistingRegistration<T>(this ComponentRegistration<T> componentRegistration) where T : class
    {
        return componentRegistration
        .Named(Guid.NewGuid().ToString())
        .IsDefault();
    }
}

我不确定 HubsInstaller 是否试图从同一个项目中执行 WTF,但我自己制作了似乎工作正常的WTF(我当然愿意接受任何建议,为什么这会很糟糕):

public class HubsInstallers : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromThisAssembly()
                            .BasedOn<IHub>()
                            .LifestyleTransient());
    }
}

这也适用于较新的 SignalR 版本 0.5+

于 2012-07-13T21:34:56.533 回答
1

鸽子的答案很好,但有点令人困惑,添加了另一个更具体的答案。我的主要目标是这样工作:

[HubName("MyHub")]
public class MyHub : Hub
{
    public IJobRepository JobRepository { get; }

    public MyHub(IJobRepository jobRepository)
    {
        JobRepository = jobRepository ?? throw new ArgumentNullException(nameof(jobRepository));
    }
...
}

当然,您想要的是为您创建集线器,它们通常由 SignalR 创建,但现在它们具有一些依赖关系,SignalR 无法创建它们。SignalR 本身有一个 Dependency Resolver(在 SignalR 命名空间中),用于获取自己的依赖项,您可以向其中添加内容,但我们希望 Windsor 还记得吗?所以我们将改变IHubActivator创建集线器的方式,我们不会使用 SignalR,而是使用这个:

public class SignalRHubActivator : IHubActivator
{
    private readonly IWindsorContainer _container;

    public SignalRHubActivator(IWindsorContainer container)
    {
        _container = container;
    }

    public IHub Create(HubDescriptor descriptor)
    {
        var result = _container.Resolve(descriptor.HubType) as IHub;
        if (result is Hub)
        {
            _container.Release(result);
        }
        return result;
    }
}

要在 SignalR 容器中替换它,您必须执行以下操作:

// Get an instance of the hub creator (see note below)
var _hubActivator = new SignalRHubActivator(container);

// Get the SignalR's Default Dependency Resolver
var signalRResolver = new Microsoft.AspNet.SignalR.DefaultDependencyResolver();
// Override the IHubActivator service
signalRResolver.Register(typeof(IHubActivator), () => _hubActivator);
// now map SignalR with this configuration
appBuilder.MapSignalR(new HubConfiguration { Resolver = signalRResolver });

就是这样,您还应该在 Windsor 注册您的所有集线器

container.Register(Classes.FromThisAssembly()
    .BasedOn(typeof(Microsoft.AspNet.SignalR.Hub)));
...
container.Register(Component.For<IJobRepository>()).ImplementedBy<JobRepository>());

注意:我也将 SignalRHubActivator 注册为组件,这是因为Startup我使用的类接收激活器作为依赖项:

container.Register(Component.For<SignalRHubActivator>().
    DependsOn(Dependency.OnValue("container", container)));
于 2017-09-29T16:34:42.807 回答