如何解决 WCF 中的 System.ServiceModel.ServerTooBusyException?

Posted

技术标签:

【中文标题】如何解决 WCF 中的 System.ServiceModel.ServerTooBusyException?【英文标题】:How to solve System.ServiceModel.ServerTooBusyException in WCF? 【发布时间】:2016-04-22 12:33:41 【问题描述】:

我正在开发一个每天平均访问​​量为 50,000 次(大约 140,000 次页面浏览量)的网站。 我使用WCF 作为我的服务,InstanceContextMode = InstanceContextMode.PerCall 另外,我正在使用异步方法来调用 WCF 服务, 最近我在某些页面和某些请求上收到以下异常消息:

Exception Type: System.ServiceModel.ServerTooBusyException
Exception Message: The HTTP service located at http://localhost:8090/SmartService.svc is unavailable.  This could be because the service is too busy or because no endpoint was found listening at the specified address. Please ensure that the address is correct and try accessing the service again later.
Stack Trace:    at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass5`1.<CreateGenericTask>b__4(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Smartiz.ClientServices.ContentService.<GetContentsByIdsAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Smartiz.UI.Controllers.CompareController.<FillRequiredFields>d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Smartiz.UI.Controllers.CompareController.<GetDevices>d__4b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Smartiz.UI.Controllers.CompareController.<Index>d__73.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)

        =========== Inner Exception ===========
        Exception Type: System.Net.WebException
        Exception Message: The remote server returned an error: (503) Server Unavailable.

serviceBehaviors改成下面这样可以吗? 以下是一个好的解决方案吗?

主机> 身份> 端点> 服务> 服务> 行为> 服务行为> 行为> 安全> 绑定> 绑定>

我的意思是serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647"

【问题讨论】:

增加并发调用固然好,但它可能只会延迟不可避免的事情。您的 WCF 服务执行什么样的操作?我看到你正在使用async 来调用它。但是,如果您的服务随后执行任何其他 I/O,它们也应该是 async。例如数据库;文件等 您使用 serviceThrottling 并且工作了吗? WCF ServiceIIS Sever 的性能改进了吗? 【参考方案1】:

首先检查应用程序池是否正常。 如果您的应用程序在一段时间内产生过多异常,您将收到相同的错误消息。

根据我的经验 - 如果相关属性 maxConcurrentInstancesmaxConcurrentSessions 像这里一样正确设置 - 发生此错误时 web.config 中有问题。

【讨论】:

@mohammad-dayyan 在中心位置捕获所有异常会更容易。有两个重要的接口:IErrorHandlerIServiceBehavior。看看它并检查是否有任何未处理的异常。如果不是,那么我们必须更深入地研究。有时 WCF 不会完全加载您的配置,并且在运行时缺少一些绑定。如果加载失败,讨论配置是没有意义的;-)

以上是关于如何解决 WCF 中的 System.ServiceModel.ServerTooBusyException?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过wcf服务更新宿主项目中的winform控件?

如何解决 .NET Core 客户端应用程序中的 WS2007HttpBinding WCF 服务绑定

如何在虚拟机中使用托管在 Windows 服务中的 WCF

一个WCF服务中的更改应更新相同WCF服务的其他实例中的相同更改

WCF 中的 MaxReceivedMessageSize 错误

如何解决此 WIF/WCF 异常?