My program has taken to generating the occasional NullReferenceException in the call to System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf method. Here's the statement that causes the exception to be thrown: CarSystem.Read read = context.Reads.Where( r => r.ReadId == Id && r.InstanceId == instanceId ).FirstOrDefault(); Here's the stack trace: at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) at System.Data.EntityClient.EntityConnection.Open() at System.Data.Objects.ObjectContext.EnsureConnection() at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) at LPRCore.CarSystem.ReadDataAccessor.GetReadInstance(CarSystemEntities context, Guid Id, Guid instanceId, Int32 dataTypeid) in c:\ElsagTFS\EOC4\Client\LPRCore Plugin CarSystem\ReadDataAccessor.cs:line 348 Most of the time this exception does not occur. The What would cause the exception to be thrown? We are using EBF 3967. asked 28 Jan '14, 17:00 TonyV |
We found the cause of this problem. It turns out another developer had modified some code to save the parameters from a previous call to try again later if that call threw an exception. He saved a reference to the context object, which was actually disposed when the previous call exited. He then passed that reference to the function which has the issue. Since that context was disposed of, we get the error. Moral of the story: Don't save references to objects that get disposed when the function they were instantiated in exits! answered 29 Jan '14, 09:52 TonyV |