on 2014 Jan 28 5:00 PM
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 context
object is created by the caller of the method where the line is and is passed as a parameter and checked for null before the line is executed. In fact, the connection to the database should already be opened by the time this method is called. Also, Id & instanceId
are Guids
and cannot be null.
What would cause the exception to be thrown? We are using EBF 3967.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
66 | |
10 | |
10 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.