I am using ASP.NET FileStreamResult to send back a file as a response to the user.
It works fine on sending any file, but after calling some functions using System.Drawing to generate some files, it stuck and never send a response to the frontend. The frontend is keeping "pending for the request".
And looking at the code below, I specify once response is sent, the file will be deleted, but the file is still on the server side, that means the file is never sent out. I then stop the server, and the file is deleted on the server side, that means when I stop the server, the response is fulfilled at that time.
I already tested long request processing time and large size of file, and found they doesn't matter here. The only matter is the call to functions of System.Drawing. So I create another thread to run those functions and this problem is solved and the response can be sent out.
My question is Why calling System.Drawing and System.Windows.Forms will break ASP.NET??
I am using IIS Express as the server. also using .NET 6.
protected ActionResult SendFile(string filePath)
{
// generate images
System.Windows.Forms.DataVisualization.Charting.Chart oCh = new();
oCh.SaveImage(filePath, System.Drawing.Imaging.ImageFormat.Gif);
// fetch image
FileStream content = new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.None, 4096, options: FileOptions.DeleteOnClose);
// File stream will be disposed after response is sent
var re = this.File(content, contentType, filename);
return re;
}