使用StructureMap将依赖项注入到SignalR 2.0.1 [英] Using StructureMap for dependency injection to SignalR 2.0.1
问题描述
我正在尝试使用StructureMap将依赖项注入到SignalR集线器中.
I'm trying to use StructureMap for dependency injection to a SignalR hub.
Many sources in the internet say this should be done as in this answer: How do you Resolve signalR v2.0 with StructureMap v2.6. I tried it, and got it to work - at least for the first action after the first pageload.
当我尝试离开包含SignalR-JS-Code的HTML页面(或重新加载页面)时,或者当我第二次使用集线器中定义的功能之一时,出现此StructureMapException:You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead.
在HubActivator
When I try to leave the HTML page that includes the SignalR-JS-Code (or reload the page), or when I use one of the functions defined in my hub a second time, I get this StructureMapException: You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead.
in the public IHub Create(HubDescriptor descriptor)
function of my HubActivator
我已经通过在引导过程中修改扫描尝试了此操作:
I already tried this by modifying my scan during bootstrapping:
container.Configure(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.AssembliesFromApplicationBaseDirectory(GetFilteredAssemblies);
scan.WithDefaultConventions().OnAddedPluginTypes(t => t.LifecycleIs(InstanceScope.Hybrid));
scan.LookForRegistries();
scan.AddAllTypesOf<MyProject.Data.Common.IEntity>();
scan.AddAllTypesOf<IMappedEntity>();
scan.AddAllTypesOf<IDatabaseInitializer>();
scan.AddAllTypesOf<IBootstrapMember>();
scan.AddAllTypesOf<IMembership>();
});
});
但这没有帮助.
要解决此异常,我必须进行什么更改(在SignalR或StructureMap中)?
What do I have to change (in SignalR or StructureMap) to fix this exception?
推荐答案
它被淘汰了,我不得不从
It tourned out I had to change my NHibernateRegistry from
[...]
if (HttpContext.Current != null)
{
For<ISession>()
.HttpContextScoped()
.Use(x => x.GetInstance<ISessionFactory>().OpenSession());
}
[...]
到
[...]
if (HttpContext.Current != null)
{
For<ISession>()
.HybridHttpOrThreadLocalScoped()
.Use(x => x.GetInstance<ISessionFactory>().OpenSession());
}
[...]
不同的上下文似乎可以避免会话丢失.
The different context seems to avoid the session getting lost.
这篇关于使用StructureMap将依赖项注入到SignalR 2.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!