Coder Social home page Coder Social logo

Comments (4)

sgorozco avatar sgorozco commented on June 26, 2024 2

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.

from microsoft.io.recyclablememorystream.

sgorozco avatar sgorozco commented on June 26, 2024

from microsoft.io.recyclablememorystream.

vitali-karmanov avatar vitali-karmanov commented on June 26, 2024

If I remove the using and I added pdfFileStream.Seek(0, SeekOrigin.Begin) before the return it works.

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);

var memoryStream = manager.GetStream();

memoryStream.Write(pdfFileAttachment.FileBytes, 0, pdfFileAttachment.FileBytes.Length);

pdfFileStream.Seek(0, SeekOrigin.Begin);

return new FileStreamResult(memoryStream, pdfFileAttachment.ContentType);

However, doesn't removing the using kind of defeat the purpose of the RecyclableMemoryStream by not disposing the stream?

Thank you!

from microsoft.io.recyclablememorystream.

steve-warren avatar steve-warren commented on June 26, 2024

FileStreamResult is automatically disposed per FileStreamResultExecutor.ExecuteAsync:

https://github.com/dotnet/aspnetcore/blob/d9df235c2e6d41b65d8e860c5e209d9df8cbcae1/src/Mvc/Mvc.Core/src/Infrastructure/FileStreamResultExecutor.cs#L31

from microsoft.io.recyclablememorystream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.