1
votes

We're working with Entity Framework in a Windows Azure environment and till now we were using InProc session state, but as some of you might know, due to it's distributed nature we should use another method, that's why we activated the Azure AppFabric Cache.

When activating this session state provider we started having the following exception:

ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.]
       System.Data.Objects.ObjectContext.EnsureConnection() +11658009
       System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +66
       System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption) +271
       System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() +499
       System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty(TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject) +136
       System.Data.Objects.Internal.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__1(TProxy proxy, TItem item) +153
       System.Data.Entity.DynamicProxies.SysUser_1A4439A55EAE70AD5C976139AA3A390B54A2C96E5FA605B3F364F0ADF52D0707.get_Assignments() +151
       WriteSysUser_1A4439A55EAE70AD5C976139AA3A390B54A2C96E5FA605B3F364F0ADF52D0707ToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) +544
...

This exception seems to be thrown when the AppFabric tries to serialize an EF object to cache.

We're using EF with LazyLoading and ProxyCreation flags both active, and this does not seem to be supported in this scenario, but it was in InProc Session state management.

We're looking for some suggestions on how to use Azure AppFabric Session State provider with EF, while maintaining lazy loading.

Thanks, Rui

1

1 Answers

3
votes

You cannot use lazy loading and dynamic proxies with any kind of session. If it worked with InProc it was either due to invalid handling of EF context lifetime or you was just lucky and accessed the cached object only in scope of its context.

Lazy loading requires living context and context should live only to perform single unit of work. Because of that you cannot have requirement to support lazy loading on cached entities. Cached entities should not be proxied (proxy doesn't make sense for them) at all and must be detached.