我在C#.NET 3.5 CompactFramework中有一个程序,该程序从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();
            }
}


我可以使用不同的参数(WebServer上的不同控制器)多次调用此方法,然后突然一切都停滞了。当我在Visual Studio中按“暂停”时,应用程序不再响应

using (Stream requestStream = request.GetRequestStream())


有时,System.Net.Connection ...的Timer.ring中会引发SystemException,即使万一应用程序无法继续运行,即使不通过将异常冒泡到下一个catch-Block也会继续运行。这意味着,我必须重置永远不会继续运行的设备。

我尝试了以下更改来解决问题:


request.KeepAlive = true / false
request.Pipelines = true / false
ServicePointManager.DefaultConnectionLimit = 1000;
request.AutomaticDecompression = DecompressionMethods.GZip或不执行任何操作


没什么,例如在Postman中,请求工作正常。
奇怪的是,如果我在for循环中实现此功能,要求更新约200个对象,则崩溃速度更快。如果我在按钮单击上实现request方法,然后以大约10秒的频率单击它,则它的工作时间会更长。我尝试在端口888上开发IIS后端,并在端口80上使用生产计算机,本地防火墙已关闭。没有任何失败的请求,可能是对A或B或C类型的请求,...每次运行都不同。

有人会解释:

a)为什么代码卡住并且无法继续?

b)为什么即使抛出异常也会使代码卡住

c)如何配置ServicePointManager或Request使其正常工作

编辑:这是在执行request.GetRequestStream()时有时发生的异常:

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()

最佳答案

我在这个问题上也停留了几天。最后,我发现,如果我在后台运行Fiddler,则request.GetRequestStream()中不会引发任何异常。这意味着这与提琴手正在处理的连接池有关。所以我做了一些研究,发现下面的链接解决了我的问题:

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

同样,在请求完成后,请确保也中止该请求。我所做的是:

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


对我来说,现在一切都很好。

关于c# - 多次调用后,HttpWebRequest GetRequestStream SystemException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45507806/

10-13 23:20