Windows Mobile: Exception in Finalizer terminates program

My Windows application for Windows sometimes fires with an exception that occurs in the finalizer System.Net.HttpReadStream.

This only happens occasionally, but then it resets the entire program. Is there anything I can do to keep the program going when such an internal finalizer is thrown? Or alternatively, how can I prevent such an error?

Here's the stacktrace exception (not complete, since I have to introduce all this ...

ObjectDisposedException

at System.Threading.Timer.throwIfDisposed()
at System.Threading.Timer.Change(UInt32 dueTime, UInt32 period)
at ...
at System.Net.ContentLengthReadStream.doClose()
at System.Net.HttpReadStream.Finalize()

Call Code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(actionUrl);
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.ContentType = "application/json";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(writer, myRequestObject);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        JsonSerializer serializer = new JsonSerializer();
        MyResultObject result = (MyResultObject)serializer.Deserialize(reader, typeof(MyResultObject));

        return result;
    }
}

Update

The calling code above is fine. The problem was caused by another HttpWebRequestwhere the answer was not deleted. Therefore, remember to always delete the response object, but especially in the Compact Framework, since it can kill your entire application!

+3
3

, HttpWebRequest. , .

+1

thread .

, . , .

+2

(Dipsose) ResponseStream , :

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream respStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(respStream))
{
  JsonSerializer serializer = new JsonSerializer();
  MyResultObject result =
    (MyResultObject)serializer.Deserialize(reader, typeof(MyResultObject));

  return result;
}

Reader , flaky.

+1

Source: https://habr.com/ru/post/1713322/


All Articles