I am developing a process in MVC to take input from a database object and convert the data to an Excel file export. I am using NPOI tool to do this.
The current conversion process is made completely independent with no external dependencies Or Shared objects.
The excel generation works fine and generates the output as required.
The problem arises when the user initiates a second request when the first one is on progress.
When there are more than one requests the process generates the byte[] and returns to the controller to send the file to client side. But on return the file does not download. It only downloads the file related to the last request.
For example:
- when there are 3 requests initiated one after the other.
- The processes is carried out for first
- The processes is carried out for second request
- The processes is carried out for Third request
- All requests are carried out simultaneously
- Finishes Request1, Returns to the controller with Result for Request1
- Does not download the file for Request1
- Finishes Request2, Returns to the controller with Result for Request2
- Does not download the file for Request2
- Finishes Request3, Returns to the controller with Result for Request3
- DOWNLOAD's the file for Request3
How can I handle this and makes sure all generated files are downloaded as they are processed.
**Controller**
public ActionResult ExportExcel(ExportCriteriaVM exportCriteriaVM)
{
.....
var excelByteArray = _translator.CreateExcelExportFile(..., ..., ...);
return File(excelByteArray, _documentManager.GetMimeType("xlsx"), xlsxName);
}
**Translator**
byte[] IExportTranslator.CreateExcelExportFile(Param1, Param2, Param3)
{
return doSomeWork(Param1, Param2, Param3);
}
private byte[] doSomeWork(CalendarSetupVM calendarSetupVM, GanttExportCriteriaVM ganttExportCriteriaVM, string filterName)
{
using (var ms = new MemoryStream())
{
........
........
return ms.ToArray();
}
}
How can I ensure the file is downloaded for each request.
await Task.Run(() -> x())is just an inefficient version ofx().SemaphoreSlimcan prevent multiple threads from starting the same process. But perhaps you'd prefer a queue of some kind? It all depends on why the process breaks when you run it in parallel, and how you will manage the results.doSomeWork()inline and lose all the async stuff. You haven't answered my question.