Using StructureMap for dependency injection to SignalR 2.0.1

随声附和 提交于 2019-12-08 05:23:17

问题


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.

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

But this didn't helped.

What do I have to change (in SignalR or StructureMap) to fix this exception?


回答1:


It tourned out I had to change my NHibernateRegistry from

[...]
if (HttpContext.Current != null)
{
    For<ISession>()
    .HttpContextScoped()
    .Use(x => x.GetInstance<ISessionFactory>().OpenSession());
}
[...]

to

[...]
if (HttpContext.Current != null)
{
    For<ISession>()
    .HybridHttpOrThreadLocalScoped()
    .Use(x => x.GetInstance<ISessionFactory>().OpenSession());
}
[...]

The different context seems to avoid the session getting lost.



来源:https://stackoverflow.com/questions/20926728/using-structuremap-for-dependency-injection-to-signalr-2-0-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!