Selective IPrincipal Injection via StructureMap with SignalR
Asked Answered
A

1

2

StructureMap is configured to inject HttpContext.Current.User when an IPrincipal is requested for any ASP.NET MVC web request, like so:

For<IPrincipal>().Use(x => HttpContext.Current.User);

But when my SignalR hub asks for a service that depends on an IPrincipal, injection fails because HttpContext.Current is null. Instead, SignalR already has a HubCallerContext property that exposes the current IPrincipal via Context.User.

How do I configure StructureMap to always inject a valid IPrincipal into the services my SignalR hub relies on?

Autogamy answered 26/10, 2012 at 9:52 Comment(0)
B
1

Just do this:

For<IPrincipal>().Use(x => Thread.CurrentPrincipal);
Bogosian answered 26/10, 2012 at 10:1 Comment(3)
How do I retrieve the current HubCallerContext from outside of the hub? AFAIK, there is no static, global HubCallerContext.Current like there is for HttpContext.Autogamy
Ahh okay.. And you can't use the Thread.CurrentThread.CurrentPrincipal?Bogosian
Going to try Thread.CurrentPrincipal (for non-static context).Autogamy

© 2022 - 2024 — McMap. All rights reserved.