An existing connection was forcibly closed by the remote host

后端 未结 12 1843
说谎
说谎 2020-11-22 09:26

I am working with a commercial application which is throwing a SocketException with the message,

An existing connection was forcibly closed by the rem

相关标签:
12条回答
  • 2020-11-22 09:45

    For anyone getting this exception while reading data from the stream, this may help. I was getting this exception when reading the HttpResponseMessage in a loop like this:

    using (var remoteStream = await response.Content.ReadAsStreamAsync())
    using (var content = File.Create(DownloadPath))
    {
        var buffer = new byte[1024];
        int read;
    
        while ((read = await remoteStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
        {
            await content.WriteAsync(buffer, 0, read);
            await content.FlushAsync();
        }
    }
    

    After some time I found out the culprit was the buffer size, which was too small and didn't play well with my weak Azure instance. What helped was to change the code to:

    using (Stream remoteStream = await response.Content.ReadAsStreamAsync())
    using (FileStream content = File.Create(DownloadPath))
    {
        await remoteStream.CopyToAsync(content);
    }
    

    CopyTo() method has a default buffer size of 81920. The bigger buffer sped up the process and the errors stopped immediately, most likely because the overall download speeds increased. But why would download speed matter in preventing this error?

    It is possible that you get disconnected from the server because the download speeds drop below minimum threshold the server is configured to allow. For example, in case the application you are downloading the file from is hosted on IIS, it can be a problem with http.sys configuration:

    "Http.sys is the http protocol stack that IIS uses to perform http communication with clients. It has a timer called MinBytesPerSecond that is responsible for killing a connection if its transfer rate drops below some kb/sec threshold. By default, that threshold is set to 240 kb/sec."

    The issue is described in this old blogpost from TFS development team and concerns IIS specifically, but may point you in a right direction. It also mentions an old bug related to this http.sys attribute: link

    In case you are using Azure app services and increasing the buffer size does not eliminate the problem, try to scale up your machine as well. You will be allocated more resources including connection bandwidth.

    0 讨论(0)
  • 2020-11-22 09:45

    I had the same issue and managed to resolve it eventually. In my case, the port that the client sends the request to did not have a SSL cert binding to it. So I fixed the issue by binding a SSL cert to the port on the server side. Once that was done, this exception went away.

    0 讨论(0)
  • 2020-11-22 09:46

    I've got this exception because of circular reference in entity.In entity that look like

    public class Catalog
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public Catalog Parent { get; set; }
        public ICollection<Catalog> ChildCatalogs { get; set; }
    }
    

    I added [IgnoreDataMemberAttribute] to the Parent property. And that solved the problem.

    0 讨论(0)
  • 2020-11-22 09:46

    If Running In A .Net 4.5.2 Service

    For me the issue was compounded because the call was running in a .Net 4.5.2 service. I followed @willmaz suggestion but got a new error.

    In running the service with logging turned on, I viewed the handshaking with the target site would initiate ok (and send the bearer token) but on the following step to process the Post call, it would seem to drop the auth token and the site would reply with Unauthorized.

    It turned out that the service pool credentials did not have rights to change TLS (?) and when I put in my local admin account into the pool, it all worked.

    0 讨论(0)
  • 2020-11-22 09:48

    I got the same issue while using .NET Framework 4.5. However, when I update the .NET version to 4.7.2 connection issue was resolved. Maybe this is due to SecurityProtocol support issue.

    0 讨论(0)
  • 2020-11-22 09:50

    Had the same bug. Actually worked in case the traffic was sent using some proxy (fiddler in my case). Updated .NET framework from 4.5.2 to >=4.6 and now everything works fine. The actual request was:
    new WebClient().DownloadData("URL");
    The exception was:

    SocketException: An existing connection was forcibly closed by the remote host

    0 讨论(0)
提交回复
热议问题