I'm attempting to create a custom DataReader for an IAsyncEnumerable<T>
collection in order to load the records into a database table via SqlBulkCopy
. I'm following along the lines of the code example and solution outlined in this question - How to use SqlBulkCopy to write an IAsyncEnumerable
Here is the gist of my DataReader:
internal sealed class AsyncEnumerableDataReader<T> : IDataReader
{
private readonly IAsyncEnumerator<T> _asyncEnumerator;
private readonly List<PropertyInfo> _properties = new List<PropertyInfo>();
private bool _isClosed = false;
public AsyncEnumerableDataReader(IAsyncEnumerable<T> asyncEnumerable)
{
_asyncEnumerator = asyncEnumerable.GetAsyncEnumerator();
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
_properties.Add(propertyInfo);
}
}
//.... Other method implementations here
public bool Read() => _asyncEnumerator.MoveNextAsync().Result;
}
My issue is that when I pass the datareader into the SqlBulkCopy.WriteToServerAsync(reader)
method, on the first call to DataReader.Read()
it throws the following error:
System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'
Does anyone have any idea as to where I'm going wrong with my code?