使用 Web 服务时出错:现有连接被强制关闭
我有一个用 C# 编写的 Winforms 应用程序,它使用来自 Windows 2008 IIS Coldfusion 服务器的 Web 服务。所有 Web 服务调用都成功,只有一个调用成功,大约 50% 的情况下会失败,并出现以下错误:
System.InvalidOperationException 未被用户代码处理 消息 = XML 文档中存在错误 (1254, 7)。
有一个内部异常:
InnerException: System.IO.IOException 消息=无法从传输连接读取数据:现有连接被远程主机强制关闭。
我检查了 IIS 日志,收到 503 错误(服务不可用)和 IIS 代码 64(指定的网络不再可用)。任何建议都会很棒。
我在 SOAP UI 中运行 Web 服务,收到以下错误:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection Reset
此代码一次运行正常公司,但对于我目前正在使用的这家公司,几乎每次都会弹出此错误。
I have a Winforms appplication written in C# that consumes web services from a Windows 2008 IIS Coldfusion server. All the web service calls succeed but one, which fails about 50% of the time with the following error:
System.InvalidOperationException was unhandled by user code
Message=There is an error in XML document (1254, 7).
with an inner exception of:
InnerException: System.IO.IOException
Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
I checked my IIS logs and I get a 503 error (Service Unavailable) and an IIS code of 64 (The specified network is no longer available). Any suggestions would be great.
I run my web service in SOAP UI and I get the following error:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Connection reset
This code works fine at one company but this error pops up almost every time for this company I'm currently working with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我不确定这是否适用于OP的具体情况,但这可能会对现在到达这里的其他人有所帮助。导致此异常的一个潜在原因涉及安全协议不匹配。如果您调用的服务器需要 TLS 1.2,并且您使用的是较旧版本的 ASP.net(<= 版本 4.0),则除非您更改它,否则您将使用较旧的安全协议进行调用。您可以强制 ASP.net 使用 TLS 1.2(如下所示)。这可以在应用程序中的任何位置完成,但我将其放在调用需要 TLS 1.2 的 Web 服务的行之前:
I'm not sure this is applicable to the OP's specific situation, but this may help others who arrive here nowadays. One potential cause for this exception involves mismatched security protocols. If the server you're calling requires TLS 1.2 and you're using an older version of ASP.net (<= Version 4.0) you will be using an older security protocol to make your calls unless you change it. You can force ASP.net to use TLS 1.2 (shown below). This can be done anywhere in the application, but I put it just before the line that calls the web service requiring TLS 1.2:
我最近在使用 WCF-Webservice 时收到了类似的消息。就我而言,它变成了
是服务器端的配置错误。也许有些东西配置不同
在您发生这种情况的一台服务器上?
我的问题是默认的最大消息大小配置得太小
在服务器上,这导致了相同的强制连接关闭。有一个默认值
避免 DOS 攻击的最大消息大小...
I recently got a similar message when consuming a WCF-Webservice. In my case it turned
out to be a configuration error on the server side. Maybe something is configured differently
on the one server where this happens to you?
My problem was that the default maximum message size was configured to be too small
on the server and this resulted in the same forced connection closing. There is a default
maximum message size to avoid DOS attacks...
如果您使用 WCF 客户端连接到服务,请使用以下配置在客户端应用程序中启用服务跟踪日志记录:
下载 windows sdk,您就可以使用一个很好的跟踪查看器来查看这些日志文件。它可以帮助您深入了解 WCF 通信中的错误。
If you are using a WCF client to connect to the service, enable service trace logging in your client application with the following config:
Download the windows sdk and you have a nice trace viewer for these log files. It helps you get to the bottom of errors in WCF communication.
使用跨平台通信有时会发生(曾经发生在我的代码中)抛出的异常并不是内部发生的情况的真实描述。
导致此异常的一个原因是您的响应时间略小于 Web 服务方法完成所需的时间。因此,请尝试增加 app.config 中的超时。
如果它不起作用,则您的情况可能存在两个问题。
但我希望仅仅增加超时就能解决这个问题。
Using cross platform communication sometimes it happens (once happened to my code) that the exception thrown is not the real description of what is happening inside.
One cause of this exception is that your response time is a little lesser than time required by the webservice method to complete. So try to increase the timeout in your app.config.
If it doesn't work there could be two possible problems in your case.
But I hope just increasing the timeout will fix this.
我确实遇到了类似的错误,原因是 XML 序列化中出现异常。大多数情况下,如果 xmlserializer 尝试读取某些属性,并且该 get 方法由于某些数据库连接已关闭或任何资源不可用而引发异常。
您是否尝试过在 global.asax 内的错误事件中记录异常?
有时,如果 global.asax 不引发错误事件,则只有通过响应过滤器记录错误的方法。您可以在 web.config 中添加自定义响应过滤器,在其中您将能够分析正确序列化的 XML 数量以及可能失败的位置。
http://msdn.microsoft.com/en-us/library/aa479332.aspx
http://www.raboof.com/projects/elmah/
I did get similar error and cause was exception in XML serialization. Mostly if xmlserializer tries to read some property and that get method raises an exception due to some database connection already closed or any resources not available.
Have you tried to log exceptions in error event inside global.asax?
Sometimes if global.asax does not raise error event, then only way to log error through response filter. You can add custom response filter in the web.config, in which you will be able to analyze how much XML was correctly serialized and where it might be failing.
http://msdn.microsoft.com/en-us/library/aa479332.aspx
http://www.raboof.com/projects/elmah/
中级 仅来自一个目标的“现有连接被远程主机强制关闭”对我来说听起来像是一个网络问题。
尝试从您尝试访问的服务器以及两个位置的相关防火墙获取日志。
您可以运行 Fiddler 或 NetMon / WireShark / Ethereal 进行进一步诊断。
Intermediate "An existing connection was forcibly closed by the remote host" from only one destination sounds like a networking issue to me.
Try getting logs from the server you try to access and from the involved firewalls of both locations.
You may run Fiddler or NetMon / WireShark / Ethereal to diagnose further.
在任何情况下都会发生连接关闭。确保服务器和客户端的超时时间充足,并确保返回的数据没有递归。循环引用。在这种情况下,序列化很重要,因为返回时正在对事物进行序列化。
执行 WCF 跟踪程序并检查那里的答案。服务器中的任何故障都会关闭连接。如果服务器需要用户名,请确保这些信息正确。请注意 SSL 错误。使用WCF客户端来测试服务。
Connection close happen for any circumstance. Make sure the timeout is abundant on the server and the client, make sure there is no recursion in the data you are returning. Circular reference. Serialization is important in this case because the thing is being serialized when returned.
Do a WCF tracer and check the answer there. Any fault in the server will close the connection. If the server requires username, make sure those are correct. Take care of the SSL error. use WCF client to test the service.
这可能是盲目的,但这是我的理论:
第一个错误发生在 Web 服务端,抛出了一个糟糕的异常,也许一些无效数据正在传递到服务中?这可能会返回有关 XML 格式错误的错误。我会做几个测试用例来查看哪些数据被传递到服务中以及导致问题的原因。
我之前在某种情况下看到的第二个错误是关于抛出 Web 服务异常以及围绕该服务的 using 语句的 try catch。这种逻辑组合导致了未清理的提前退出。
This may be a shot in the dark but here is my theory:
The first error is happening on the web service side with a poor exception being thrown, maybe some invalid data is being passing into the service? This could return the error regarding the XML being malformed. I would do several test cases to see what data is being passed into the service and what causes the issue.
The second error I have seen before in a certain circumstance regarding a web service exception being thrown and a try catch wrapped around a using statement for the service. This combination of logic caused an early exit that wasnt cleaned up.
尝试检查您上一家公司的现有协议并将其与您当前的公司进行比较,我的意思是 TCP/Ip,...
try to check the existing protocols in your last company and compare them with your current company,I mean TCP/Ip,...
检查 IIS 中的应用程序池回收配置。我见过这个错误,例如,当“私有内存限制”设置为一个值(比如 100mb),然后 w3wp 进程超过此限制,这将导致应用程序池被回收。
这通常不是问题,因为任何现有连接都有时间完成,并且新连接将由新启动的应用程序池处理。
如果所有连接未在关闭时间限制(通常为 90 秒)内关闭,则它们将被 IIS 终止,并且客户端可能会引发“现有连接被强制关闭”错误。
Check the app pool recycling configuration in IIS. I have seen this error, for example, when the "Private Memory Limit" is set to a value (say 100mb) and then the w3wp process exceeds this limit which will cause the app pool to be recycled.
This normally isn't a problem since any existing connections are given time to complete and new connections will be processed by the newly spun-up app pool.
If all the connections are not closed within the shutdown time limit though (normally 90 seconds) then they are killed by IIS and the client may raise the "An existing connection was forcibly closed" error.