开发者

Wanna use StructureMap to store HttpContext/User based explicit instances

开发者 https://www.devze.com 2022-12-28 09:50 出处:网络
I\'m having difficulty figuring out how to store an explicitly user generated instance in StructureMap, cached by HttpContext.

I'm having difficulty figuring out how to store an explicitly user generated instance in StructureMap, cached by HttpContext. When I try the code underneath, I even get the first cached instance, which leads to failures when using it for storing user credentials in Asp.Net AuthenticateRequest method.

ForRequestedType<TInterface>()
                            .CacheBy(InstanceScope.HttpContext)
                                .TheDefault.
      开发者_如何学C                              Is.
                                        Object(instance));

The problem is I can't create a new instance on requesting StructureMap, because I need more other factories for getting rights etc. for the current user.

Any ideas?


Here is my registry config that I use for an ASP.Net MVC app

For<ICurrentUser>().HybridHttpOrThreadLocalScoped().Use<CurrentUser>();

I configure the CurrentUser instance after checking the authentication status of the user. This happens in an authorization action filter. Each subsequent type needing ICurrentUser receives the properly setup instance as it is cached scoped to the web request.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号