I have a sync method that calls an async method that returns a Stream.
The async method makes a call to web api and returns a stream that represents a document.
Whenever I call the async method from within my sync method, the stream is returned as closed.
If I call the async method from within another async method, the stream is returned opened.
I can't call the async method from within another async method because I have to implement an interface from an external library and the code that calls the async method is done from within a sync method that's defined in the interface.
So - SyncMethod -> AsyncMethod -> WebApi returns a closed stream
AsyncMethod1 -> AsyncMethod -> WebApi returns an open stream.
Here is the signature for my async method -
public async Task<Response<Stream>> GetSomeDocument()
In my sync method, I call the async method like so -
var task = this.someClass.GetSomeDocument();
task.Wait();
This is where it blows up and throws ObjectDisposedException -
var memoryStream = new MemoryStream();
task.Result.CopyTo(memoryStream);
Any idea why this works if I call my async method from within an async method using await, but calling it from a sync method doesn't?
Thanks.