gpt4 book ai didi

.net - 将 CasTLe Windsor 与 SignalR 集成 - 我应该如何处理这个问题?

转载 作者:行者123 更新时间:2023-12-02 03:27:31 26 4
gpt4 key购买 nike

我正在开始使用 SignalR,一旦配置完所有内容,它就可以很好地工作。然而,我开发的几乎所有应用程序都使用 CaSTLe Windsor,因此如果能够将它们一起使用那就太好了。我想这样做的原因是我可以在持久连接内使用 CaSTLe 依赖项/服务。

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

以下哪一个是更好的主意?是否有另一种方法可以用来组合 CaSTLe 和 SignalR?

谢谢,埃里克

最佳答案

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,但为了便于阅读,暂时不提供。

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

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

关于.net - 将 CasTLe Windsor 与 SignalR 集成 - 我应该如何处理这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com