只是想补充一点,在添加了这个问题中给出的代码之后,我没有发现这个错误再次发生。我认为垃圾收集做到了。
我正在努力处理上述错误。我有两个 asp.net web 应用程序。其中一个被视为服务器,另一个被视为客户端。我正在使用 Hessian 编码方法来处理 Web 请求和响应。
最初,我的客户端和服务器 100% 正确地相互响应。但一段时间后,我的客户端停止联系我的服务器,我收到此错误:
请求被中止:操作已超时
我已经尝试了很多我在互联网上找到的东西来解决我的问题。我做的第一件事是关闭请求和响应流。
然后我添加了以下属性:
request.KeepAlive = false;
request.Timeout = 5000;
request.Proxy = null;
request.ServicePoint.ConnectionLeaseTimeout = 5000;
request.ServicePoint.MaxIdleTime = 5000;
之后我添加了 ReadWriteTime 和 GC.Collect()。
但我所有的努力都没有结果。
此问题仅发生在 GoDaddy 服务器上,我的项目在我的本地服务器和另一台物理服务器上运行良好。从本地主机运行项目时,我也没有收到此错误。
仅当我部署到我的 GoDaddy 服务器或我将本地客户端连接到我的 GoDaddy 服务器时才会发生这种情况。
从日志中我可以看到请求停止到达服务器端。
这是我的最终代码:
// Send Request to Edge Server. Will receive a response as an Encoded stream
String infoMessage = "userId Is: " + request.Info.UserId.AsString() + " Session Id is: " + request.Info.SessionId.AsString();
String logMessage = "inside Invoke: " + infoMessage;
Stream requestStream = null;
HttpWebResponse webResponse = null;
Stream responseStream = null;
HttpWebRequest webRequest=null;
try
{
// Construct web request
logMessage = "Creating webrequest to: " + url + " " + infoMessage;
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = encoderType.Trim().ToLower();
webRequest.Method = REQUEST_METHOD;
webRequest.Timeout = 120000;
webRequest.ReadWriteTimeout = 120000;
webRequest.KeepAlive = false;
webRequest.Proxy = null;
webRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
webRequest.ServicePoint.MaxIdleTime = 5000;
logMessage = logMessage + "Timeout is 5000 Keep Alive False";
logMessage = "timout is: " + webRequest.Timeout.ToString() + logMessage;
// Encode Request object as Stream
requestStream = webRequest.GetRequestStream();
EncoderFactory.GetInstance(this.encoderType).Encode(requestStream, request);
logMessage = "going to getresponse" + logMessage;
webResponse = (HttpWebResponse)webRequest.GetResponse();
logMessage = "Response returend" + logMessage;
// Recieve web response
String contentType = webResponse.ContentType;
logMessage = "going to get response stream" + logMessage;
responseStream = webResponse.GetResponseStream();
// If response Content Type is 'text/html' then assume it's an error message
if (contentType.ToLower().Trim().StartsWith("text/html"))
{
StreamReader reader = new StreamReader(responseStream, Encoding.Default);
String errorMessage = reader.ReadToEnd();
reader.Close();
return new ErrorResponse(-1000, logMessage + " " + errorMessage);
}
else if (contentType.ToLower().Trim().StartsWith(EncoderFactory.ENCODER_TYPE_HESSIAN))
{
// Decode Response object from Stream
return (Response)EncoderFactory.GetInstance(this.encoderType).Decode(responseStream);
}
else
{
return new ErrorResponse(-1001, "Invalid response - ContentType='" + contentType + "'");
}
}
catch (Exception ex)
{
return new ErrorResponse(-1002, "Exception is -1002 and Message is = " + ex.Message + " " + logMessage + ".");
}
finally
{
// Close request stream
if (requestStream != null)
{
try
{
requestStream.Flush();
requestStream.Close();
}
catch { }
}
// Close web response
if (webResponse != null)
{
try
{
webResponse.Close();
}
catch { }
}
// Close response stream
if (responseStream != null)
{
try
{
responseStream.Flush();
responseStream.Close();
}
catch { }
}
GC.Collect();
}
}