C# 已建立的连接被主机中的软件中止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1608431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:20:20  来源:igfitidea点击:

An established connection was aborted by the software in your host machine

c#

提问by TheCodeFool

Sorry if this is a bit long winded but I thought better to post more than less.

对不起,如果这有点啰嗦,但我认为发布更多比更少更好。

This is also my First post here, so please forgive.

这也是我在这里的第一篇文章,所以请原谅。

I have been trying to figure this one out for some time. and to no avail, hoping there is a genius out there who has encountered this before.

一段时间以来,我一直试图弄清楚这一点。并无济于事,希望有一个以前遇到过这种情况的天才。

This is an intermittent problem and has been hard to reproduce. The code that I am running simply calls a web service The Web Service call is in a loop (so we could be doing this a lot, 1500 times or more)

这是一个间歇性问题,很难重现。我正在运行的代码只是调用 Web 服务 Web 服务调用处于循环中(所以我们可以这样做很多次,1500 次或更多)

Here is the code that is causing the error:

这是导致错误的代码:

HttpWebRequest groupRequest = null;
WebResponse groupResponse = null;            
try
{    
    XmlDocument doc = new XmlDocument();
    groupRequest = (HttpWebRequest)HttpWebRequest.Create(String.Format(Server.HtmlDecode(Util.GetConfigValue("ImpersonatedSearch.GroupLookupUrl")),userIntranetID));
    groupRequest.Proxy = null;
    groupRequest.KeepAlive = false;
    groupResponse = groupRequest.GetResponse();
    doc.Load(groupResponse.GetResponseStream());
    foreach (XmlElement nameElement in doc.GetElementsByTagName(XML_GROUP_NAME))
    {
         foreach (string domain in _groupDomains )
         {
             try
             {
                 string group = new System.Security.Principal.NTAccount(domain, nameElement.InnerText).Translate(typeof(System.Security.Principal.SecurityIdentifier)).Value;
                 impersonationChain.Append(";").Append(group);                            
                 break;
             }
             catch{}                        
         } // loop through            
     }
 }
 catch (Exception groupLookupException)
 {
     throw new ApplicationException(String.Format(@"Impersonated Search ERROR:  Could not find groups for user<{0}\{1}>", userNTDomain, userIntranetID), groupLookupException);
 }
 finally
 {
     if ( groupResponse != null )
     {
         groupResponse.Close();
     }
 }

Here is the error that happens sometimes:

这是有时发生的错误:

Could not find groups for user<DOMAIN\auser> ---> System.IO.IOException: Unable to read
data from the transport connection: An established connection was aborted by the
software in your host machine. ---> System.Net.Sockets.SocketException: An established  
connection was aborted by the software in your host machine at  
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags  
socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32  
size) --- End of inner exception stack trace --- at System.Net.ConnectStream.Read(Byte[]  
buffer, Int32 offset, Int32 size) at System.Xml.XmlTextReaderImpl.ReadData() at  
System.Xml.XmlTextReaderImpl.ParseDocumentContent() at  
System.Xml.XmlLoader.LoadDocSequence  
(XmlDocument parentDoc) at System.Xml.XmlDocument.Load(XmlReader reader) at
System.Xml.XmlDocument.Load(Stream inStream) at  
MyWebServices.ImpersonatedSearch.PerformQuery(QueryParameters parameters,  
String userIntranetID, String userNTDomain)--- End of inner exception stack trace  
---at MyWebServices.ImpersonatedSearch.PerformQuery(QueryParameters parameters, String userIntranetID, String userNTDomain)  
--- End of inner exception stack trace ---  
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message,  
WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName,  
Object[] parameters) at MyProgram. MyWebServices.ImpersonatedSearch.PerformQuery  
(QueryParameters parameters, String userIntranetID, String userNTDomain)  
at MyProgram.MyMethod()

Sorry that was alot of code to read through.
This happens about 30 times out of around 1700

抱歉,需要阅读大量代码。
这种情况在大约 1700 次中发生了大约 30 次

采纳答案by jfawcett

You're probably hitting a timeout. First of all, turn the keepalive back on. Second, check the timestamps on the request and reply. If there is a firewall between the sender and receiver, make sure that it isn't closing the connection because of idle timeout. I've had both these problems in the past, although with generic socket programming, not DB stuff.

您可能正在超时。首先,重新开启keepalive。其次,检查请求和回复的时间戳。如果发送方和接收方之间有防火墙,请确保它不会因为空闲超时而关闭连接。我过去曾遇到过这两个问题,尽管使用通用套接字编程,而不是 DB 的东西。