在异步使用的HttpWebRequest上调用Abort时出现ObjectDisposedException

时间:2010-12-14 02:02:23

标签: c# .net httpwebrequest objectdisposedexception

正如标题所示,我在异步使用的ObjectDisposedException上调用"Abort"似乎得到了HttpWebRequest(即BeginGetResponse),而不是我的生活,弄清楚如何防止它。我花了一整天寻找解决方案,所以任何帮助将不胜感激。这是一个说明问题的简单示例:

// helper class that gets passed through as the state
class RequestState
{
    public HttpWebRequest Request { get; set; }
    public bool TimedOut { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var rs = new RequestState
        {
            Request = (HttpWebRequest)WebRequest.Create("http://google.com")
        };

        var result = rs.Request.BeginGetResponse(
            asyncResult =>
            {
                if (asyncResult.IsCompleted)
                {
                    var reqState = asyncResult.AsyncState as RequestState;

                    if (reqState != null && !reqState.TimedOut)
                    {
                        using (var response = reqState.Request.EndGetResponse(asyncResult) as HttpWebResponse)
                        {
                            using (var streamReader = new StreamReader(response.GetResponseStream()))
                            {
                                Console.WriteLine(streamReader.ReadToEnd());
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Timed out!");
                    }
                }
            },
            rs);

        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
            (state, timeout) =>
            {
                if (timeout)
                {
                    var temprs = state as RequestState;
                    if (temprs != null)
                    {
                        // set TimedOut flag
                        temprs.TimedOut = true;
                        // this will cause the BeginGetResponse callback above to be called
                        temprs.Request.Abort();
                    }
                    // after this method leaves scope the ObjectDisposedException occurs!
                }
            },
            // time out of 7 seconds
            rs, 7000, true);

        Console.ReadLine();


    }
}

这是我正在做的事情: 如果我正常运行,响应就会很好地写入控制台。但是,如果我使用Fiddler模​​拟慢速互联网连接(或基本上没有连接)并执行超时回调,我会得到前面提到的ObjectDisposedException"Timed out!"首先写入控制台)。如果我不在Abort上致电HttpWebRequest,我就不会收到此异常。

谁能告诉我我做错了什么?我的目标是.NET 3.5框架。提前感谢您的任何启示。

这是异常信息/调用堆栈:

System.ObjectDisposedException occurred
    Message=Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
    Source=System
    ObjectName=System.Net.Sockets.NetworkStream
    StackTrace:
        at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
    InnerException: 

System.Net.Sockets.NetworkStream.EndRead(System.IAsyncResult asyncResult) + 0x1b7 bytes 
System.Net.PooledStream.EndRead(System.IAsyncResult asyncResult) + 0x10 bytes   
System.Net.Connection.ReadCallback(System.IAsyncResult asyncResult) + 0x33 bytes    
System.Net.Connection.ReadCallbackWrapper(System.IAsyncResult asyncResult) + 0x46 bytes 
System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) + 0x69 bytes   
System.Net.ContextAwareResult.Complete(System.IntPtr userToken) + 0xab bytes    
System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken) + 0xb0 bytes 
System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped) + 0x94 bytes    
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* pOVERLAP) + 0x54 bytes

0 个答案:

没有答案
相关问题