Selective IPrincipal Injection via StructureMap wi

2019-05-28 13:40发布

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?

1条回答
男人必须洒脱
2楼-- · 2019-05-28 14:14

Just do this:

For<IPrincipal>().Use(x => Thread.CurrentPrincipal);
查看更多
登录 后发表回答