Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top