Call to Dispose throws error on ObjectContext.Connection

Jun 29, 2014 at 6:32 AM
In a webforms project using Autofac I am getting an error on Dispose whereby objectContext.Connection throws a ObjectDisposedException.

I can work round it with a catch block but I was more curious as to why this is happening.

IUnitOfWork and DbContext are both registered with InstancePerHttpRequest lifetime scope.
 The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Source Error:


Line 29:         public void Dispose()
Line 30:         {
Line 31:             if (_objectContext != null && _objectContext.Connection.State == ConnectionState.Open)
Line 32:             {
Line 33:                 _objectContext.Connection.Close();


Source File: c:\Dev\GenericUnitOfWork\Source\Repository.Pattern.Ef6\UnitOfWork.cs    Line: 31

Stack Trace:


[ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.]
   System.Data.Entity.Core.Objects.ObjectContext.get_Connection() +69
   Repository.Pattern.Ef6.UnitOfWork.Dispose() in c:\Dev\GenericUnitOfWork\Source\Repository.Pattern.Ef6\UnitOfWork.cs:31
   Autofac.Core.Disposer.Dispose(Boolean disposing) +81
   Autofac.Util.Disposable.Dispose() +46
   Autofac.Core.Lifetime.LifetimeScope.Dispose(Boolean disposing) +65
   Autofac.Util.Disposable.Dispose() +46
   Autofac.Integration.Web.ContainerProvider.EndRequestLifetime() +19
   Autofac.Integration.Web.ContainerDisposalModule.OnEndRequest(Object sender, EventArgs e) +24
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
This happens outside of a unitOfWork Transaction which is Committed correctly.
Jun 30, 2014 at 9:10 AM
Hi, jafin:

This is the same behaviour I reported as an issue:

https://genericunitofworkandrepositories.codeplex.com/workitem/1226

EM
Marked as answer by lelong37 on 7/3/2014 at 8:21 AM
Coordinator
Jul 3, 2014 at 5:44 PM
Marked as answer by lelong37 on 7/3/2014 at 9:44 AM