WPF client dropping REST connections

时间:2018-02-26 17:50:35

标签: c# wpf rest httpwebrequest dotnet-httpclient

Update:

This is unrelated to WPF, as all clients are failing now. Using WireShark, I see that the server is sending packets but they are not being received by the client, and the server eventually sends an RST packet to kill the connection (which never makes it to the client).


WPF seems to be closing HTTP connections if a REST service takes a long time to respond.

I'm using a REST web service that takes 7 minutes to generate a JSON response. When called from Firefox, Android, or iOS, I see the response. When called from WPF's HttpWebRequest (or HttpClient or WebClient) with a 10 minute timeout, the web service reports "Connection reset by peer" when it tries returning the JSON after 7 minutes, and at the 10 minutes mark, the HttpWebRequest throws a timeout error.

Here is one version of the code:

try
{
    // Create Request
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

    // Set timeouts
    request.Timeout = 10 * 60 * 1000;
    request.ReadWriteTimeout = 10 * 60 * 1000;
    request.ContinueTimeout = 10 * 60 * 1000;

    // Getting desperate (I also tried not doing any of this)
    request.KeepAlive = true;
    request.ProtocolVersion = HttpVersion.Version10;
    request.ServicePoint.ConnectionLeaseTimeout = 10 * 60 * 1000;
    request.ServicePoint.MaxIdleTime = 10 * 60 * 1000;

    // Attempt to get data
    WebResponse response = request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader sr = new StreamReader(stream);
    string content = sr.ReadToEnd();
    label1.Content = content.Length.ToString();
}
catch (Exception ee)
{
    // End up here
    label1.Content = ee.Message;
}

I have also tried HttpClient, WebClient, and RestSharp with the same results.

If the service responds quickly, WPF gets the data correctly. In a fit of depiration, I also used regedit to set TcpTimedWaitDelay to 19 minutes, just in case.

Here is what I think is happening:

  • minute 0: WPF makes a request to the REST web service
  • sometime: WPF (or something on its behalf) sends an RST packet to the server to kill the connection (from What does "connection reset by peer" mean?)
  • minute 7: REST web service finishes processing, reads RST packet, and closes the connection with no response
  • minute 10: WPF timeout expires after no response, throws exception

Does anyone out there know what's going on or how I can debug this?

Thanks!

0 个答案:

没有答案