c# - WCF 服务调用总是在 30 秒后失败,并显示 (502) Bad Gateway

标签 c# asp.net wcf

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

System.Net.WebException: The remote server returned an error: (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 Trace Viewer(请参阅答案)帮助发现问题不是服务本身,而是客户端和 wcf 服务之间的问题。

最佳答案

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

关闭超时、打开超时、接收超时。

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

Client side:

  • SendTimeout is used to initialize the OperationTimeout, which governs the whole interaction for sending a message (including receiving a reply message in a request-reply case). This timeout also applies when sending reply messages from a CallbackContract method.
  • OpenTimeout and CloseTimeout are used when opening and closing channels (when no explicit timeout value is passed).

Server side:

  • Send, Open, and Close Timeout same as on client (for Callbacks).
  • ReceiveTimeout is used by ServiceFramework layer to initialize the session-idle timeout.

添加:

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

关于c# - WCF 服务调用总是在 30 秒后失败,并显示 (502) Bad Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2765732/

相关文章:

c# - PKCS#11 在本地主机上工作,但它不在实时 Web 服务器(客户端计算机)上工作

javascript - 从 UpdatePanel 内的用户控件注入(inject) javascript

WCF 可序列化实体和 k__BackingField

C#限制字符串长度

c# - 如何查看<returns>标签的内容?

asp.net - IIS 不会缓存大 (76MB) 文件

TeamCity 中的 WCF 集成测试

c# - 使用 Cookie 或其他方法保护 WFC/JSON 上的数据 session

c# - 我需要使用 opencvsharp 识别图像中的文本

c# - 在 Visual Studio Enterprise 2017 中缺少创建 GUID?