ClosedXML / ClosedXML/ClosedXML.Extensions.AspNet
Exception Thread was being aborted
- Dominant language
- C#
- Stars
- 27
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Description
I am using latest version 1.0.5 of ClosedXML.Extensions.AspNet
I have a static class ExcelService containging this code:
public static void CreateExcelSheetForDataTableAndAddToResponse(DataTable tableToHandle, HttpResponse response, string fileName)
{
DataSet ds = new DataSet("REPORTS");
ds.Tables.Add(tableToHandle);
var wb = new XLWorkbook();
wb.Worksheets.Add(ds);
if (wb.Worksheets.FirstOrDefault() != null && wb.Worksheets.FirstOrDefault().Columns() != null)
{
wb.Worksheets.FirstOrDefault().Columns().AdjustToContents();
}
fileName = fileName.EndsWith(".xlsx") ? fileName : fileName + ".xlsx";
//wb.DeliverToHttpResponse(response, fileName);
response.DeliverWorkbook(wb, fileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
I call it like this from my code behind for a asp.net page:
ExcelService.CreateExcelSheetForDataTableAndAddToResponse(myDataTable, this.Response, fileName.ToFileName());
My string extension for safe file name generation:
public static string ToFileName(this string s)
{
string regexSearch = new string(Path.GetInvalidFileNameChars());
Regex rs = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
s = rs.Replace(s, "");
return s.Replace(" ", "_"); // replace space with underscore
}
The response stream gets the file with both delivery methods but both delivery methods also throws a Thread was being aborted exception. Catching this exception explicitly and it all works. But why is it throwing that Thread exception?
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the call to CreateExcelSheetForDataTableAndAddToResponse with both DeliverWorkbook and DeliverToHttpResponse in an ASP.NET page. Read the response-delivery paths and trace where the ThreadAbortException originates; done means determining whether it is expected response termination or an extension bug, with the behavior and handling made clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100