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

Moh*_*yan 5 c# wcf exception wcf-configuration asynchronous-wcf-call

我正在开发一个每天平均有 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.
Run Code Online (Sandbox Code Playgroud)

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

  <系统.服务模型>
    <服务>
      <服务名称=“Smartiz.WcfService.SmartService”行为配置=“ServiceBehavior”>
        <主机>
          <基地址>
            <添加baseAddress =“http://localhost/”/>
          </基本地址>
        </主机>
        <端点地址=“”绑定=“basicHttpBinding”合同=“Smartiz.WcfService.ISmartService”绑定配置=“basicHttpBinding”>
          <身份>
            <dns 值=“本地主机”/>
          </身份>
        </端点>
        <端点地址=“mex”绑定=“mexHttpBinding”合同=“IMetadataExchange”/>
      </服务>
    </服务>
    <行为>
      <服务行为>
        <行为名称=“ServiceBehavior”>
          <serviceThrottting maxConcurrentCalls =“2147483647”maxConcurrentInstances =“2147483647”maxConcurrentSessions =“2147483647”/>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </行为>
      </服务行为>
    </行为>
    <协议映射>
      <添加方案=“https”绑定=“basicHttpsBinding”/>
      <添加方案=“http”绑定=“basicHttpBinding”/>
    </协议映射>
    <serviceHostingEnvironment aspNetCompatibilityEnabled =“true”multipleSiteBindingsEnabled =“true”minFreeMemoryPercentageToActivateService =“0”/>
    <绑定>
      <基本Http绑定>
        <绑定名称=“basicHttpBinding”maxReceivedMessageSize=“2147483647”messageEncoding=“文本”textEncoding=“utf-8”maxBufferPoolSize=“2147483647”>
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <安全模式=“无”>
          </安全>
        </绑定>
      </basicHttpBinding>
    </绑定>
  </system.serviceModel>

我是说serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647"

Ben*_*Ben 2

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

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