在SignalR 2.0中使用现有的IoC容器 [英] Using an existing IoC Container in SignalR 2.0
问题描述
如何在SignalR 2.0中使用现有的IoC?
How can I use an existing IoC with SignalR 2.0?
在本教程中,看来我需要设置一个通过属性从OWIN调用的类:
From the tutorial, it seems I need to setup a class to be called from OWIN via an attribute:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
public class Startup
{
public void Configuration(IAppBuilder app /*HOW AM I GONNA GET UNITY CONTAINER HERE?*/)
{
var hubConfig = new HubConfiguration()
{
EnableJSONP = true,
EnableDetailedErrors = true,
EnableJavaScriptProxies = true,
Resolver = new SignalRUnityDependencyResolver(container)
};
// Any connection or hub wire up and configuration should go here
app.MapSignalR(hubConfig);
}
}
}
这里的问题是我已经有一个容器,该容器已被捆绑了引导,并且该容器中有一些单例实例需要与在同一主机下共享的MVC应用程序共享.
The problem here is that I already have a container, that's boot strapped and there are singleton instances in the container that needs to be shared with the MVC app shared under the same host.
但是,这里的麻烦在于,与以前不同,我无法从自己的代码中调用MapSignalR
方法.相反,我需要依靠OWIN为我完成此任务.但是OWIN并不知道我已经设置的容器.
However the trouble here is that unlike before, it doesn't look like I can call the MapSignalR
method from my own code. Rather I need to rely on OWIN to do this for me. However OWIN is not aware of the container that I already setup.
解决此问题的最佳方法是什么?我有一些非常粗糙的想法,如何使用静态变量将一些解决方案结合在一起,但我对此非常讨厌.代码很脆弱,操作顺序很容易引入一个细微的错误.
What's the best way to resolve this? I have some very crude ideas how to hack a solution together using static variables to hold some of these - but I hate the very thought of it. The code will be brittle and order of operation could easily introduce a subtle bug.
是否有一种方法可以在不让OWIN调用上述方法的情况下保留IAppBuilder实例?这样,可以在SignalR初始化时更好地控制,并且可以将自己的IoC传递到配置中.
Is there a way to get a hold of the IAppBuilder instance without having OWIN invoke the above method? This way I can control better when SignalR gets initialized and I can pass my own IoC into the configuration.
推荐答案
在我的情况下,我创建了一个自定义的集线器激活器,该激活器在我的应用程序和signalR之间使用共享容器(通过构造函数注入),这样您将拥有一个复合整个应用程序的根目录.
In my case I have created a custom hub activator which uses a shared container between my app and signalR (by constructor injection) that way you´ll have single composite root for the whole application.
尝试以下操作:
public class CustomHubActivator : IHubActivator
{
private readonly Container _container;
public MseHubActivator(Container container)
{
_container = container;
}
public IHub Create(HubDescriptor descriptor)
{
return _container.GetInstance(descriptor.HubType) as IHub;
}
}
在引导应用程序(可能是global.asax)时注册您的自定义集线器激活器
register your custom hub activator when you´re bootstrapping your app (maybe the global.asax)
GlobalHost.DependencyResolver.Register(typeof (IHubActivator),
() => new CustomHubActivator(Container));
这是一个更简单的解决方案,而不是再次配置signalRdependencyResolver
that´s much simplier solution rather than to configure again the signalR dependencyResolver
这篇关于在SignalR 2.0中使用现有的IoC容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!