It is because the stream is disposed on the very next line. The call is asynchronous.. and therefore drops to the Dispose
call at the end of the using
block.
await
the Task
returned from CopyToAsync
so that the fileStream
instance is still available to it as its doing what it needs to do:
public async Task SaveFileContentsAsync(string filePath, Stream stream)
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await stream.CopyToAsync(fileStream);
}
}