使用(502)Bad Gateway后,WCF服务调用在30秒后始终失败

Jef*_*mer 6 c# asp.net wcf

我们有一个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)
Run Code Online (Sandbox Code Playgroud)

这30秒"超时"的来源或为什么会返回502 Bad Gateway错误?

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

Tan*_*ner 5

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

closeTimeout,openTimeout,recieveTimeout.

有关配置项的信息,请参阅此MSDN帖子,摘要如下:

客户端:

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

服务器端:

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

添加:

我可以建议的唯一另一件事是使用WCF服务跟踪查看器来找到导致问题的底部.如果您需要有关如何使用它的详细信息,请参阅此SO帖子.