views:

66

answers:

1

Hi

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.
                                    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?

A: 

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.

KevM
How do you add CurrentUser to the container? I assume the code above is in your registry? Are you re-Initializing your ObjectFactory for every time a user is authenticated? Sorry, I'm still learning.
mxmissile
nvm, I just found ObjectFactory.Inject(...)
mxmissile
I my code I am letting StructureMap create the object and am populating specific properties during authorization. You could construct the object yourself and use Inject to push the instance into the container and it will be scoped correctly.
KevM