Kukkimonsuta/Clutch

Disposing System.Data.EntityClient.EntityDataReader leads to an exception

Opened this issue · 1 comments

Issue

Disposing an instance of EntityDataReader class leads to rising InvalidOperationException: Reader timing has already finished .

Cause

The exception is thrown by the DbTracingContext.OnReaderFinished() method called second time from the DbTracingDataReader.Close().

In turn, DbTracingDataReader.Close() method is called twice due to the implementation of the Dispose() method in the EntityDataReader class (see the sources):

162        public override void Close()
163        {
164            if (this._command != null)
165            {
166               this._storeDataReader.Close();
167                
168               // other code
176            }
177        }
178 
183        protected override void Dispose(bool disposing)
184        {
185            base.Dispose(disposing);
186            if (disposing)
187            {
188                this._storeDataReader.Dispose();
189            }
190        }

this._storeDataReader field store an instance of the DbTracingDataReader and it is disposing twice in the lines 166 and 188. And the base Dispose() method in the line 185 internally calls virtual Close() method.

Solution

DbTracingDataReader class ьгые support multiple calls to the Close() method.

one of the fix options #7