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?
 
                        
When you use
.Resultwith aValueTask<T>it does not block the current thread to wait for the result(boolin your case).If a
ValueTask<T>does not finish in time during it's first call it will return aTaskthat the CLR will use to continue work later to check on it.The CLR can not check for the result later when you do not
awaitthe call.Since you want to run this code synchronously(at least from what I can assume by using
.Result) I have included a work around that should work for you synchronously.Consider using
.AsTask().Resultinstead of.Result. This will force the CLR to wait for the Task to finish before returning a result.