【问题标题】:WCF Service calls always fail after 30 seconds with (502) Bad GatewayWCF 服务调用总是在 30 秒后因 (502) 错误网关而失败
【发布时间】:2010-05-04 13:34:59
【问题描述】:

我们有一个 WCF 服务 (BasicHttpBinding),它总是在 30 秒后失败。呼叫在 30 秒内完成,没有错误。任何超过 30 秒的操作都会失败,并出现 502 Bad Gateway 异常:

System.Net.WebException:远程服务器返回错误:(502) Bad Gateway。

但是 WCF 调用继续在后台运行(并最终完成)。我们已经确认BasicHttpBinding-Binding-sendTimeout(在web.config中)大于30秒(实际设置为5分钟)。我们在客户端和服务器上都确认了这一点。

这是完整的堆栈跟踪:

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (502) Bad Gateway. ---> System.Net.WebException: The remote server returned an error: (502) Bad Gateway.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

任何想法这 30 秒“超时”来自何处或为什么返回 502 Bad Gateway 错误?

解决方案: 我们正在使用具有自己的代理设置的 IIS7 应用程序请求路由模块。代理设置的默认超时为 30 秒。将其增加到 600 秒(10 分钟)解决了我们的问题。 Bad Gateway 错误并不完全正确,但 WCF 跟踪查看器(请参阅答案)帮助发现问题不是服务本身,而是客户端和 wcf 服务之间的问题。

【问题讨论】:

    标签: c# asp.net wcf


    【解决方案1】:

    您可能想尝试修改其他超时配置值:

    closeTimeout、openTimeout、recieveTimeout。

    有关配置项的信息,请参阅this MSDN post,摘要如下:

    客户端:

    • SendTimeout 用于初始化 OperationTimeout,它控制发送消息的整个交互(包括 在请求-回复情况下接收回复消息)。这个超时也 从 CallbackContract 方法发送回复消息时适用。
    • OpenTimeout 和 CloseTimeout 在打开和关闭通道时使用(当没有传递明确的超时值时)。

    服务器端:

    • 发送、打开和关闭超时与客户端相同(用于回调)。
    • ServiceFramework 层使用ReceiveTimeout 来初始化会话空闲超时。

    添加:

    我唯一可以建议的另一件事是使用 WCF 服务跟踪查看器来查明导致问题的原因。如果您需要有关如何使用它的详细信息,请参阅此SO Post

    【讨论】:

    • 我们已尝试将 closeTimeout、openTimeout 和 receiveTimeout 增加到 5 分钟(在客户端和服务器上),但仍然存在问题。
    • 感谢您提供有关 WCF 服务跟踪查看器的信息。我用它来收集客户端和服务器上的跟踪,但我从中看到的是服务器正在正确完成,但客户端最终收到“收到错误的 HTTP 响应”,内部消息是“远程服务器返回意外响应:(502) 网关错误。”服务器在 42 秒后完成,客户端在 31 秒后收到错误消息。客户端始终为 31 秒。
    • 想通了。我们正在使用具有代理设置的 IIS7 应用程序请求路由模块,其中之一是 30 秒超时。使用 WCF 服务跟踪查看器确实帮助我了解 WCF 服务不是问题,而是客户端和服务之间的问题。
    【解决方案2】:

    IIS -> 高级设置 -> 连接限制

    将该数字(以秒为单位)增加到所需的数量。

    希望这对所有 Google 员工有所帮助!

    【讨论】:

      【解决方案3】:

      我今天自己也遇到了这个问题。从 SL4 应用程序将文件上传到 WCF Web 服务在 30 秒后不断引发 ConnectionTimeout 异常。

      我发现问题的原因是使用 Microsoft 推荐的 WebRequest.RegisterPrefix 方法来克服 Silverlight 中的故障异常处理:

      bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
      

      http://msdn.microsoft.com/en-us/library/ee844556(v=vs.95).aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        • 2022-07-05
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        相关资源
        最近更新 更多