HttpWebRequest GetRequestStream SystemException after several calls randomly

时间:2017-08-04 13:13:15

标签: c# exception httpwebrequest getresponsestream

I have a program in C# .NET 3.5 CompactFramework that requests data from a C# WebService:

    public SynchronisationResult<J> Get<J>(IEnumerable<DomainProxy> existingObjects, string controller, string parameters) where J : IdJsonObject)
{
            string existingObjectsJson = JsonConvert.SerializeObject(existingObjects);
            string url = GenerateUrl(controller, parameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "text/json";
            request.Method = "POST";
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Timeout = 60000;
            request.ContentLength = existingObjectsJson.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                using (var streamWriter = new StreamWriter(requestStream))
                {
                    streamWriter.Write(existingObjectsJson);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                requestStream.Close();
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                string responseData = "";
                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    responseData = streamReader.ReadToEnd();
                }
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = JsonConvert.DeserializeObject<SynchronisationResult<J>>(responseData);
                }
                else
                {
                    throw new Exception(responseData);
                }
                response.Close();
            }
}

I can call this method several times with different parameters (different controllers on the WebServer) and suddenly everything gets stuck. The application does not react anymore, when i press PAUSE in Visual Studio i see the program pointer at the location

using (Stream requestStream = request.GetRequestStream())

Sometimes a SystemException is thrown in Timer.ring of System.Net.Connection..., although in case, the application does not continue to run, even not by bubbling the exception to the next catch-Block. This means, that i have to reset the device it does never continue to run.

I have tried the following changes to solve the problem:

  • request.KeepAlive = true / false
  • request.Pipelines = true / false
  • ServicePointManager.DefaultConnectionLimit = 1000;
  • request.AutomaticDecompression = DecompressionMethods.GZip or nothing

Nothing, the request works fine in Postman for example. Weird thing is, if i implement this in a for loop, asking for about 200 objects to be updated it crashes faster. In case i implement the request method on a button click and click it with a frequency of about 10 seconds it works way longer. I tried with a development IIS backend on port 888, with a production machine on port 80, firewall locally is turned off. There is no certain request that fails it could be a request for type A or B or C,... each run is different.

Would someone explain:

a) why code gets stuck and does not continue?

b) why code gets stuck even when an exception is thrown

c) how to configure ServicePointManager or Request to get things working properly

EDIT: This is the Exception that sometimes occurs when request.GetRequestStream() is executed:

at System.Threading.Timer.startTimer(UInt32 dueTime)
at System.Threading.Timer.Change(UInt32 dueTime, UInt32 period)
at System.Threading.Timer.Change(Int32 dueTime, Int32 period)
at System.Threading.ThreadPool.QueueUserWorkItem(WaitCallback callBack, Object state, Boolean IsHttpRequest)
at System.Net.Connection.actionSending()
at System.Net.Connection.changeState(ConnectionState state)
at System.Net.Connection.transitionRequestSent(Event e)
at System.Net.Connection.processEvent(Event e)
at System.Net.Connection.actionRequestSent()
at System.Net.Connection.changeState(ConnectionState state)
at System.Net.Connection.transitionIdle(Event e)
at System.Net.Connection.processEvent(Event e)
at System.Net.Connection.submitRequest(HttpWebRequest request)
at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connGroupName)
at System.Net.HttpWebRequest.SubmitRequest()
at System.Net.HttpWebRequest.finishGetRequestStream()
at System.Net.HttpWebRequest.GetRequestStream()

1 个答案:

答案 0 :(得分:0)

我也被困在这个问题上几天了。最后我发现如果我在后台运行Fiddler,request.GetRequestStream()中没有抛出任何异常。这意味着这是与fiddler处理此事件的连接池相关的事情。所以我做了一些研究,发现以下链接解决了我的问题:

https://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app-

请求完成后,请确保您也中止了该请求。我做的是:

if (webrequest != null) webrequest.Abort();

对我而言,现在一切正常。