Selective IPrincipal Injection via StructureMap with SignalR

时光怂恿深爱的人放手 提交于 2019-12-07 18:37:32

问题


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:


Just do this:

For<IPrincipal>().Use(x => Thread.CurrentPrincipal);


来源:https://stackoverflow.com/questions/13084648/selective-iprincipal-injection-via-structuremap-with-signalr

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