0

I am trying to register an object in Autofac which is different for every user. I thought of using the current session, but I can not find a way to accomplish this. I am using webforms, so I can not use this. I try creating a Lifetimescope for my session but along with the multitenancy I am using, I can't get it to work. Also, the session_end is never thrown. So this is not an option.

I tried almost everything but it's either not the wanted behavior (I want that the object lives in the users session, so when I update the object I want it to be saved there too) or it does not work:

  • Registering HttpSessionStateWrapper in Autofac and make my object dependent on it using InstancePerOwned
  • Create a custom ContainerProvider, add a SessionLifeTime ILifetimeScope. Create a lifetimescope in Session_Start and resolve it
  • Create a SessionWrapper with a Get< T > method which will return the object from the session for me and register this object in my container.

Is there a way to resolve the same object for each user's session?

Hope somebody can help!

Community
  • 1
  • 1
Hans Leautaud
  • 1,742
  • 1
  • 19
  • 34
  • What exactly are you trying to cache per session? – Steven Sep 08 '14 at 19:29
  • I'm developing a mechanism for a sweepstake, so I want to cache the participant and the code he entered and the price he won. – Hans Leautaud Sep 09 '14 at 05:44
  • possible duplicate of [Managing AutoFac lifetime scopes per session and request in asp.net mvc 3](http://stackoverflow.com/questions/11721919/managing-autofac-lifetime-scopes-per-session-and-request-in-asp-net-mvc-3) (despite the use of web forms, the mechanism would be the same) – Travis Illig Sep 09 '14 at 15:47
  • @TravisIllig, no it is not the same because the suggested solution in the other question is only possible in MVC. – Hans Leautaud Sep 10 '14 at 11:03
  • It is the same. Swap the MVC `ILifetimeScopeProvider` reference for the web forms integration `IContainerProvider` and it's the same process. Instead of setting a dependency resolver, you set the container provider, but the risks and the general implementation are identical. – Travis Illig Sep 10 '14 at 14:37

1 Answers1

1

So you want to cache data? But that doesn't mean you must cache the component in the session as well. You could make that component transient or singleton and let it retrieve the price data from the session when needed. This simplifies your DI registration, possibly your code, and makes it much easier to verify your DI configuration as well.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • I agree (mostly because I couldn't get it to work). I'm using PostSharp's LocationInterceptionAspect to cache or retrieve from cache when addressing a property. – Hans Leautaud Sep 09 '14 at 05:57