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.

asked 28 Jan '14, 17:00

TonyV's gravatar image

TonyV
1.2k333967
accept rate: 75%

edited 28 Jan '14, 17:03


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!

permanent link

answered 29 Jan '14, 09:52

TonyV's gravatar image

TonyV
1.2k333967
accept rate: 75%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×69
×19

question asked: 28 Jan '14, 17:00

question was seen: 3,059 times

last updated: 29 Jan '14, 09:52