NullReference Exception in system.dll at System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback
-
I don't get it. I am trying to download from a series of webpages using a call to HttpWebResponse.BeginGetResponse. This is set up in a for loop, all calls are made in the main thread and I keep getting the following NullReference Exceptiion in system.dll: > system.dll System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(uint errorCode = 64, uint numBytes = 0, System.Threading.NativeOverlapped* nativeOverlapped = 1712424) + 0xa0 bytes If its not this then its a Unable to read from transport error. Can anybody help?
-
I don't get it. I am trying to download from a series of webpages using a call to HttpWebResponse.BeginGetResponse. This is set up in a for loop, all calls are made in the main thread and I keep getting the following NullReference Exceptiion in system.dll: > system.dll System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(uint errorCode = 64, uint numBytes = 0, System.Threading.NativeOverlapped* nativeOverlapped = 1712424) + 0xa0 bytes If its not this then its a Unable to read from transport error. Can anybody help?
After how many requests do you start getting
NullReferenceException
s? Just getting responses async without any checks is dangerous. There may be a limit and there are warnings about this in the .NET Framework SDK documentation.Microsoft MVP, Visual C# My Articles
-
After how many requests do you start getting
NullReferenceException
s? Just getting responses async without any checks is dangerous. There may be a limit and there are warnings about this in the .NET Framework SDK documentation.Microsoft MVP, Visual C# My Articles
Well after about 20 hours of trying every possible combination, I found out a way around this. All my requests/responses are in the main thread and I close the HttpWebResponse and the response stream as soon as I get them and I am fine. I managed to first make an initial request which gives a webpage with about 50 links. I make the 50 requests/responses on these links and this works fine. I thought I was good until I tried to repeat the process and it gives me this exception. I dont' get it since I close all the streams and all the async calls return fine. All my HttpWebResponse/Request objects are local variables in a function so they are never reused. When looking at the threads when this exception is thrown I notice that there are two threads that are blocked waiting to read the response stream, ie.
private void ProcessResponse(System.IAsyncResult state){ HttpWebResponse linkpage=(HttpWebResponse)((HttpWebRequest)state.AsyncState ).EndGetResponse(state); StreamReader stream = new StreamReader(response.GetResponseStream()); string info = stream.ReadtoEnd();//2 threads blocked here stream.Close() response.Close();
You said that getting them without checks is dangerous. What do you mean by a check? -
Well after about 20 hours of trying every possible combination, I found out a way around this. All my requests/responses are in the main thread and I close the HttpWebResponse and the response stream as soon as I get them and I am fine. I managed to first make an initial request which gives a webpage with about 50 links. I make the 50 requests/responses on these links and this works fine. I thought I was good until I tried to repeat the process and it gives me this exception. I dont' get it since I close all the streams and all the async calls return fine. All my HttpWebResponse/Request objects are local variables in a function so they are never reused. When looking at the threads when this exception is thrown I notice that there are two threads that are blocked waiting to read the response stream, ie.
private void ProcessResponse(System.IAsyncResult state){ HttpWebResponse linkpage=(HttpWebResponse)((HttpWebRequest)state.AsyncState ).EndGetResponse(state); StreamReader stream = new StreamReader(response.GetResponseStream()); string info = stream.ReadtoEnd();//2 threads blocked here stream.Close() response.Close();
You said that getting them without checks is dangerous. What do you mean by a check?Well, I see one problem: what if the file is not simply a text file? It will not have an EOF (end of file) so
StreamReader.ReadToEnd
will never return. Even streams that may appear as text may not be, and may not have an EOF. Just use aStream
and buffer the output (you don't have to do anything with it, like you're not doing forstring info
). What I mean is that creating threads wildly like this - without limiting how many threads are created - is dangerous. You should either have some mechanism that counts the number of async requests and blocks at a certain limit. An even better way is to not using async calls but to instead use aThreadPool
and queue requests synchronously (the end result is still asynchronous). TheThreadPool
limits the number of concurrent worker items (threads) and queues the rest. It also has a few additional benefits you can learn by reading about theThreadPool
class in the .NET Framework SDK documentation.Microsoft MVP, Visual C# My Articles