在从客户端方法 getRecords 调用 ReadToEnd 方法期间,我遇到了对象处理异常,该方法使用 StreamReader 与 Web 服务器通信。
对 getRecords 的第一次调用成功,只有在后续调用期间才会发生异常,因此我没有正确关闭和处理 StreamReader 和关联的 WebRequest。
我知道我可以将这两个对象包装在 using 语句中,但是它只是扩展为 try/catch/finally 语句。从下面的代码中可以看出,我正在清理 finally 子句。
因此,我要么没有做 using 语句所做的事情,要么我的 finally 语句中可能缺少其他一些东西。如果可能的话,我宁愿不使用 using 语句,因为我喜欢我的代码明确。
这是代码和相关的异常:
public int getRecords(string[] args, string[] vals)
{
List<string> urlList = BuildUrlRequestStrings(args, vals);
WebRequest request = null;
WebResponse wresponse = null;
StreamReader sr = null;
foreach (string url in urlList)
{
request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
//request.Timeout = -1;
request.Timeout = 300000;
request.Credentials = CredentialCache.DefaultCredentials;
//request.ContentType = "application/xml";
try
{
wresponse = request.GetResponse();
/*using (StreamReader sr = new StreamReader(wresponse.GetResponseStream()))
{
_recieveBuffer = sr.ReadToEnd().ToString();
}*/
sr = new StreamReader(wresponse.GetResponseStream());
_recieveBuffer = sr.ReadToEnd();
//List<T> temp = JsonConvert.DeserializeObject<List<T>>(_recieveBuffer);
List<T> temp = JsonConvert.DeserializeObject<List<T>>(
_recieveBuffer,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }
);
_recieveData.AddRange(temp);
}
catch (WebException ex)
{
if (ex.Response != null)
{
// can use ex.Response.Status, .StatusDescription
if (ex.Response.ContentLength != 0)
{
using (var stream = ex.Response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
Log.Info(FIDB.TAG1, " WSBuffer.getRecords: WEBSERVER MESSAGE: " + reader.ReadToEnd());
}
}
}
}
return -1;
}
finally
{
if (sr != null)
{
sr.Close();
sr.Dispose();
}
if (wresponse != null)
{
wresponse.Close();
wresponse.Dispose();
}
}
}
return _recieveData.Count;
}
07-02 11:32:15.076: I/<<< FI >>>(2775): StorageRelayService.RequestQueueThread: EXCEPTION: System.ObjectDisposedException: 对象在处理后被使用。07-02 11:32:15.076: I/<<< FI >>>(2775): 在 System.Net.WebConnection.BeginRead(System.Net.HttpWebRequest 请求,System.Byte[] 缓冲区,Int32 偏移量,Int32 大小, System.AsyncCallback cb, System.Object state) [0x00000] in :0 07-02 11:32:15.076: I/<<< FI >>>(2775): at System.Net.WebConnectionStream.BeginRead (System. Byte[] 缓冲区,Int32 偏移量,Int32 大小,System.AsyncCallback cb,System.Object 状态)[0x00000] in :0 07-02 11:32:15.076: I/<<< FI
(2775):在 System.Net.WebConnectionStream.Read(System.Byte[] 缓冲区,Int32 偏移量,Int32 大小)[0x00000] 在:0 07-02 11:32:15.076:I/<<< FI >>> (2775): 在 System.IO.StreamReader.ReadBuffer () [0x00000] in :0 07-02 11:32:15.076: I/<<< FI >>>(2775): 在 System.IO.StreamReader.Read (System.Char[] 缓冲区,Int32 索引,Int32 计数)[0x00000] in :0 07-02 11:32:15.076: I/<<< FI (2775): at System.IO.StreamReader.ReadToEnd () [ 0x00000] in :0 07-02 11:32:15.076: I/<<< FI >>>(2775): 在 FieldInspection.Shared.Buffer.WSBuffer
1[FieldInspection.Shared.Model.AggregateRoot.Parcel].getRecords (System.String[] args, System.String[] vals) [0x00000] in <filename unknown>:0 07-02 11:32:15.076: I/<<< FI >>>(2775): at FieldInspection.Shared.Repository.REST.RepositoryREST
1[FieldInspection.Shared.Model.AggregateRoot.Parcel].Read(IConditions 条件)[0x00000] in :0 07-02 11:32:15.076: I/<<< FI >>>(2775): 在 FieldInspection.Shared .Model.DataAccess.ParcelRepositoryREST.parcelByIdList (System.Collections.Generic.List1 parcelIdList, Boolean bCurrent, Boolean bHistorical) [0x00000] in <filename unknown>:0 07-02 11:32:15.076: I/<<< FI >>>(2775): at FieldInspection.Droid.StorageRelayService.ProcessRequestGetParcelCache (FieldInspection.Shared.Database.IPC.Request request) [0x00000] in <filename unknown>:0 07-02 11:32:15.076: I/<<< FI >>>(2775): at FieldInspection.Droid.StorageRelayService.ProcessRequestFromForegroundActivity (System.Collections.Generic.List
1 reqList) [0x00000] in :0 07-02 11:32:15.076: I/<<< FI >>>(2775): 在 FieldInspection。 Droid.StorageRelayService.RequestQueueThread () [0x00000] in :0