Search code examples
excelasp.net-web-apiasync-awaittasknpoi

TaskCanceledException thrown on retrieving a MemoryStream with NPOI workbook


When I hit this API, a TaskCanceledException is thrown with an unhelpful stack trace. I'm using this library: https://npoi.codeplex.com/

public class TestController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Test()
        {
            var templateWorkbook = new XSSFWorkbook();
            var sheet = templateWorkbook.CreateSheet("TestSheet");
            IRow dataRow = sheet.CreateRow(4);
            var cell = dataRow.CreateCell(0);
            cell.SetCellValue(77);

            Stream memoryStream = new MemoryStream();
            templateWorkbook.Write(memoryStream);

            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(memoryStream)
            };
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "test.xlsx" };
            return httpResponseMessage;
        }
    }

What am I doing wrong? This is my stack trace:

[TaskCanceledException: A task was canceled.]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +165
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Web.Http.Owin.d__20.MoveNext() +666
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) +25 System.Web.Http.Owin.d__0.MoveNext() +1814
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +561
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +561
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 Microsoft.Owin.Cors.d__0.MoveNext() +856
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext() +185 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


Solution

  • I found a solution.

    Copying the memory stream to another one and resetting the seek position to 0 seems to fix it:

    MemoryStream memoryStream = new MemoryStream();
    templateWorkbook.Write(memoryStream);
    MemoryStream copy = new MemoryStream(memoryStream.ToArray());
    copy.Seek(0, SeekOrigin.Begin);
    

    Seems like a bug in the NPOI library to me...