当我点击此API时,会抛出一个无用的堆栈跟踪的TaskCanceledException。我正在使用这个库: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;
}
}
我做错了什么?这是我的堆栈跟踪:
[TaskCanceledException:任务被取消。]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+165
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
System.Web.Http.Owin.d__20.MoveNext()+ 666 6 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)+25 System.Web.Http.Owin.d__0.MoveNext()+1814
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
Microsoft.Owin.Security.Infrastructure.d__0.MoveNext()+561
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
Microsoft.Owin.Security.Infrastructure.d__0.MoveNext()+561
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58 Microsoft.Owin.Cors.d__0.MoveNext()+856
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+137
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+58
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)+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
答案 0 :(得分:0)
我找到了解决方案。
将内存流复制到另一个并将搜索位置重置为0似乎解决了这个问题:
MemoryStream memoryStream = new MemoryStream();
templateWorkbook.Write(memoryStream);
MemoryStream copy = new MemoryStream(memoryStream.ToArray());
copy.Seek(0, SeekOrigin.Begin);
看起来像NPOI库中的一个错误...