-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FileStreamResult: ObjectDisposedException: Cannot access a disposed object. #336
Comments
I think you should remove the using() statement surrounding the
manager.GetStream() call - it is automatically disposing the stream
before the calling function has a chance to receive the FileStreamResult
you are returning.
…On Wed, Feb 28, 2024 at 6:13 PM Vitali Karmanov ***@***.***> wrote:
Hello,
What is the right way of using a RecyclableMemoryStreamManager with
FileStreamResult? I'm having the following issue:
image.png (view on web)
<https://github.com/microsoft/Microsoft.IO.RecyclableMemoryStream/assets/40288109/6b439dc8-aed1-440f-9940-6c692bafebf7>
public class PdfFileAttachment
{
public byte[] FileBytes { get; set; }
public string ContentType { get; set; }
}
PdfFileAttachment pdfFileAttachment = await _pdfService.GetPdfFileAttachment();
var options = new RecyclableMemoryStreamManager.Options()
{
BlockSize = 1024,
LargeBufferMultiple = 1024 * 1024,
MaximumBufferSize = 16 * 1024 * 1024,
GenerateCallStacks = true,
AggressiveBufferReturn = true,
MaximumLargePoolFreeBytes = 16 * 1024 * 1024 * 4,
MaximumSmallPoolFreeBytes = 100 * 1024,
};
var manager = new RecyclableMemoryStreamManager(options);
using (var memoryStream = manager.GetStream())
{
memoryStream.Write(pdfFileAttachment.FileBytes, 0, pdfFileAttachment.FileBytes.Length);
return new FileStreamResult(memoryStream, pdfFileAttachment.ContentType);
}
The end goal is to return a pdf file that is stored in a byte[].
Thank you!
—
Reply to this email directly, view it on GitHub
<#336>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABRR7UX2EOBHM336KWSJQU3YV7BY5AVCNFSM6AAAAABD67C27GVHI2DSMVQWIX3LMV43ASLTON2WKOZSGE3DAMBZGQ2DCOA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
If I remove the using and I added pdfFileStream.Seek(0, SeekOrigin.Begin) before the return it works.
However, doesn't removing the using kind of defeat the purpose of the RecyclableMemoryStream by not disposing the stream? Thank you! |
When you dispose of the stream, the recyclable buffers, which act as storage for the stream, are returned to their pool. It's important to dispose of the stream, but make sure to do this after you've processed the FileStreamResult you're returning. The Dispose() method call, whether explicit or implicit through the using() statement, should be made from outside the method that creates and returns the FileStreamResult. |
|
Hello,
What is the right way of using a RecyclableMemoryStreamManager with FileStreamResult? I'm having the following issue:
The end goal is to return a pdf file that is stored in a byte[].
Thank you!
The text was updated successfully, but these errors were encountered: