4

How to set session scope for plugin in StructureMap 2.6?

In previous versions it is done this way:

For<ISomeObject>().CacheBy(StructureMap.InstanceScope.HttpSession).Use<SomeObject>();

However, Visual Studio displays a warning telling that the CacheBy method is deprecated, and that LifecycleIs method can be used instead.

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116

2 Answers2

4

The syntax in 2.6 is:

c.For<ISomeObject>().LifecycleIs(new HttpSessionLifecycle()).Use<SomeObject>();
PHeiberg
  • 29,411
  • 6
  • 59
  • 81
-1

I believe this will do the trick:

    For<ISomeObject>()
        .HttpContextScoped()
        .Use<SomeObject>();
ozczecho
  • 8,649
  • 8
  • 36
  • 42
  • I'll try it, and let you know. Thanks. – Guillermo Gutiérrez Apr 11 '13 at 14:12
  • It seems that it doesn't save it for session scope, because I don't have the object available for the next request. Is there any difference between Session scope and HttpContext scope? – Guillermo Gutiérrez Apr 11 '13 at 14:51
  • 3
    @guillegr123 - the difference between the `HttpSessionLifecycle` and the `HttpContextLifecycle` is that the `HttpContextLifecycle` caches the created objects per Http Request (each new http request gets a new object), while the `HttpSessionLifecycle` caches objects per Http Session (each user session gets a unique object that stays the same while the session lasts). – PHeiberg Apr 12 '13 at 07:21