来自HttpReadStream.Finalize()的另一个ObjectDisposedException [英] Another ObjectDisposedException from HttpReadStream.Finalize()

查看:64
本文介绍了来自HttpReadStream.Finalize()的另一个ObjectDisposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


在尝试完成HttpReadStream时,我一直在gc线程上遇到ObjectDisposedException

 ObejectDisposedException 
Sytstem.Threading.Timer.throwIfDisposed()
Sytstem.Threading.Timer.Change(UInt32dueTime,UINT32周期)
Sytstem.Threading.Timer.Change(UInt32dueTime,UINT32周期)
Sytstem.Net.HttpWebRequest.startReadWriteTimer()
Sytstem.Net.HttpWebRequest.ConnectionClient.Read(Byte [] data,Int32 offset,Int32 length)
Sytstem.Net.HttpReadStream.NetworkRead(Byte [] data ,Int32 offset,Int32 length)
Sytstem.Net.ContentLengthReadStream.doRead(Byte [] buffer,Int32 offset,Int32 length)
Sytstem.Net.HttpReadStream.ReadToDrain(Byte [] buffer,Int32 offset, INT32长度)
Sytstem.Net.HttpReadStream.doClose()
Sytstem.Net.ContentLengthReadStream.doClose()
Sytstem.Net.HttpReadStream.Finalize()

此问题已在本论坛的几个主题中进行了讨论:

 http://social.msdn.microsoft.com/Forums/en/netfxcompact/thread/276092ad-3724-4e79-81a6-511adb8bbc86 

http ://groups.google.com/group/microsoft.public.dotnet.framework.compactframework/browse_thread/thread/44ea29515c5cd2b0?pli = 1

http://social.msdn.microsoft.com/论坛/ zh / vssmartdevicesvbcs / thread / 28cc3007-ca4e-4f59-b610-8e5e3307ceb4

我已经尝试了他们所有的建议,但没有任何帮助。  问题仍然​​存在。



这是我的代码:

 public static string SendWebPOST(string strUrl,string strPostData){
//定义获取数据所需的变量
HttpWebRequest hwRequest = null;
string strResponse = null;

//创建WebRequest对象并获取Response流
Uri myUri = new Uri(strUrl);

#if PocketPC
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
#endif
hwRequest =(HttpWebRequest)WebRequest.Create(myUri);
hwRequest.Method =" POST" ;;
hwRequest.ContentType =" application / x-www-form-urlencoded" ;;
hwRequest.ContentLength = strPostData.Length;
hwRequest.KeepAlive = false; //这就是为了给PHP WEB服务器打电话!

//发送发布数据。使用(StreamWriter sw = new StreamWriter(hwRequest.GetRequestStream())){
sw.Write(strPostData);在StreamWriter
上调用MAKE SURE CLOSE;
//sw.Flush();
sw.Close();
}

//使用(HttpWebResponse hwResponse =(HttpWebResponse)hwRequest.GetResponse())读取响应
{
try {
using(StreamReader) sr = new StreamReader(hwResponse.GetResponseStream())){
strResponse = sr.ReadToEnd();
}
}
finally {
hwResponse.Close();
}
}

//返回响应
return strResponse;
}

我还缺少什么?  



谢谢。


解决方案

尝试此更改:

 

StreamWriter sw = new StreamWriter hwRequest GetRequestStream ());
sw
strPostData ) ;

...

 

然后将sw.Close()移动到控制流的某个地方,也许移到finally块中。

如果异常仍然 发生尝试移动它。


Hello,

I keep getting ObjectDisposedException on the gc thread when trying to finalize HttpReadStream

ObejectDisposedException
Sytstem.Threading.Timer.throwIfDisposed()
Sytstem.Threading.Timer.Change(UInt32dueTime, Uint32 period)
Sytstem.Threading.Timer.Change(UInt32dueTime, Uint32 period)
Sytstem.Net.HttpWebRequest.startReadWriteTimer()
Sytstem.Net.HttpWebRequest.ConnectionClient.Read(Byte[] data, Int32 offset, Int32 length)
Sytstem.Net.HttpReadStream.NetworkRead (Byte[] data, Int32 offset, Int32 length)
Sytstem.Net.ContentLengthReadStream.doRead(Byte[] buffer, Int32 offset, Int32 length)
Sytstem.Net.HttpReadStream.ReadToDrain (Byte[] buffer, Int32 offset, Int32 length)
Sytstem.Net.HttpReadStream.doClose()
Sytstem.Net.ContentLengthReadStream.doClose ()
Sytstem.Net.HttpReadStream.Finalize()

This issue has been discussed in a few threads on this forum:

http://social.msdn.microsoft.com/Forums/en/netfxcompact/thread/276092ad-3724-4e79-81a6-511adb8bbc86

http://groups.google.com/group/microsoft.public.dotnet.framework.compactframework/browse_thread/thread/44ea29515c5cd2b0?pli=1

http://social.msdn.microsoft.com/Forums/zh/vssmartdevicesvbcs/thread/28cc3007-ca4e-4f59-b610-8e5e3307ceb4

I've tried all their suggestions, but nothing helps.  The problem still occurs.

Here is my code:

public static string SendWebPOST(string strUrl, string strPostData) {
            //Define the variables needed to grab the data
            HttpWebRequest hwRequest = null;
            string strResponse = null;

            //Create the WebRequest object and get the Response stream
            Uri myUri = new Uri(strUrl);

#if PocketPC
            ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
#endif
            hwRequest = (HttpWebRequest)WebRequest.Create(myUri);
            hwRequest.Method = "POST";
            hwRequest.ContentType = "application/x-www-form-urlencoded";
            hwRequest.ContentLength = strPostData.Length;
            hwRequest.KeepAlive = false; //THIS MUST BE HERE FOR THE CALL TO THE PHP WEB SERVER!

            // Send the Post Data. MAKE SURE CLOSE is called on StreamWriter
            using (StreamWriter sw = new StreamWriter(hwRequest.GetRequestStream())) {
                sw.Write(strPostData);
                //sw.Flush();
                sw.Close();
            }

            // Read the Response
            using (HttpWebResponse hwResponse = (HttpWebResponse)hwRequest.GetResponse()) {
                try {
                    using (StreamReader sr = new StreamReader(hwResponse.GetResponseStream())) {
                        strResponse = sr.ReadToEnd();
                    }
                }
                finally {
                    hwResponse.Close();
                }
            }

            //Return the response
            return strResponse;
        }

What am I still missing?  

Thanks.

解决方案

Try this change:

StreamWriter sw = new StreamWriter(hwRequest.GetRequestStream()); sw.Write(strPostData);

...

Then move sw.Close(); somewhere down the control flow stream, perhaps into finally block.

If exception still occurs try moving it around.


这篇关于来自HttpReadStream.Finalize()的另一个ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆