I have a project which consists of Blazor Wasm (.NET 5.0) front-end and Asp.NET Core back-end API (.NET 5.0) to serve the front. I'm making POST call from front-end to API to export single or multiple .xlsx files to single .pdf document and then send the .pdf file back to front-end as byte array (byte[]) with some other information. The .xlsx files are located in wwwroot folder of the API.
Pdf creation works as it should and i'm able to get the file downloaded to the browser just fine. The problem is that during .pdf creation resources get stuck in the memory. I have attached a demo of this behavior. The size of the .xlsx file that gets opened in demo is roughly 149kb and the resulting .pdf file 77 kb. The actual method i'm using in my app is more complicated but this demo is enough to demonstrate the described behavior.
Here is the controller and that POST endpoint that gets called. IFormService contains the actual logic for creating .pdf file. Memory snapshot #1 is taken before
"CreateExampleFile" -method call and #2 just before exiting endpoint method (indicated by breakpoints in screenshot)
Here is the actual logic where .pdf file gets generated. Mapper is instance of AutoMapper.
Memory snapshot #2 compared to snapshot #1 shows 6,7 Mb of memory leak that mostly consists of memory streams created by syncfusion library.
What have i tried?
- opening .xlsx file to memory stream
- different styles of using encapsulations around MemoryStream/FileStream
- letting using block alone dispose PdfDocument
- manually calling PdfDocument.Close() with true and false parameters with and without using blocks
- many others
None of these have worked. The memory leak is also accumulating (every time that endpoint gets called, more resources get stuck in it).
Please help.
br. Ville