简体   繁体   中英

Dispose MemoryStream in Async Callback function

I am using AWS S3 Async upload function to upload a file to S3.

tu.BeginUpload(ur, new AsyncCallback(FinishedAsyncUpload),ms);

ms = Memory Stream

tu.InputStream = ms

My problem is that I want to dispose the Stream after the file has been uploaded. I create a callback function called FinishedAsyncUpload . I also pass the MemoryStream (ms) and dispose it in this function.

 protected void FinishedAsyncUpload(IAsyncResult result)
    {
        ((MemoryStream)result.AsyncState).Dispose();
    }

My problem is that when I try to dispose the memory stream in the callback function, I see that the object doesn't contain any data and asking for length throws ( System.objectDisposedException ).

How can I dispose the memoryStream after the file has been uploaded?

I am admittedly not familiar with the AWS API specifically, but typically System.ObjectDisposedException is only thrown when Dispose() is called on an already-disposed object. So it may be that the AWS API is disposing of the processed stream for you.

From MSDN for reference :

ObjectDisposedException Class

The exception that is thrown when an operation is performed on a disposed object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM