压力测试的OperationCanceledException

时间:2016-11-21 08:44:04

标签: c# asp.net asp.net-web-api azure-cloud-services

我刚刚制作了一个WebApi(C#,。net 4.5.2)并将其发布到网上。为了确保它正常工作,我开始测试它。

REST网络服务失败了#34;压力"测试。我每秒发送服务30 + - http请求,并收到这个典型的错误消息:

System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Net.Http.HttpContentExtensions.<ReadAsAsyncCore>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.ModelBinding.FormatterParameterBinding.<ExecuteBindingAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.HttpActionBinding.<ExecuteBindingAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()

日志中多次出现相同的错误。

当我进入特定的机器时,我看到CPU处于100%状态且RAM处于80%。这意味着,机器能够最好地处理负载。

问题

  1. 根据我上面提供的StackTrace,我无法理解 我的代码中究竟出现了什么问题?
  2. 我的代码会自动扩展(托管在云端)。但是,新机器仅在5分钟后创建(取决于平均CPU> 50%)。我应该如何处理突发的HTTP请求?也许服务器应该说:&#34;嘿,在30秒内试试agian&#34;或类似的东西?什么是正确的解决方案?

1 个答案:

答案 0 :(得分:2)

System.OperationCanceledException: The operation was canceled.

这通常表示连接到服务的客户端在服务发送响应之前关闭了连接。就api层而言,这通常意味着您在建立http连接时指定了超时,或者您用于进行http调用的库具有默认超时。

您可能需要检查iis请求日志以查看出现了什么问题。由于这是在压力测试中发生的,我的猜测是你的服务缺少线程(阅读this了解更多细节)。您可能需要考虑请求处理程序的异步模型,以提高可伸缩性并更好地利用线程。 (阅读this