AutoMapper / AutoMapper.Extensions.ExpressionMapping

MIT License
143 stars 39 forks source link

Enumerator not disposed when arbitrary projection is enumerated #133

Closed acjh closed 2 years ago

acjh commented 2 years ago

The enumerator obtained from SingleQueryingEnumerable is not disposed (and thus _datareader.Dispose() is not called) when an arbitrary projection (select) is enumerated:

https://github.com/AutoMapper/AutoMapper.Extensions.ExpressionMapping/blob/4f66fc2bd99b94dae8b64bd2fe7653f92f8700fa/src/AutoMapper.Extensions.ExpressionMapping/Impl/SourceInjectedQueryProvider.cs#L113-L132

More info: https://stackoverflow.com/questions/4982396/does-foreach-automatically-call-dispose

This causes an exception if the underlying transaction is later committed or rolled back.

Stack trace from https://stackoverflow.com/questions/71161821/getting-odata-dto-automapper-and-unitofwork-to-play-nicely-in-aspnetboilerplate:

``` System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first. at Microsoft.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) at Microsoft.Data.SqlClient.SqlInternalTransaction.Rollback() at Microsoft.Data.SqlClient.SqlInternalTransaction.Dispose(Boolean disposing) at Microsoft.Data.SqlClient.SqlInternalTransaction.Dispose() at Microsoft.Data.SqlClient.SqlTransaction.Dispose(Boolean disposing) at System.Data.Common.DbTransaction.Dispose() at Microsoft.EntityFrameworkCore.Storage.RelationalTransaction.Dispose() ... ```