我在 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 中按 PAUSE 时,我在该位置看到程序指针
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 个对象,它会更快地崩溃。如果我在单击按钮时实现请求方法并以大约 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()