0

I realise that similar questions have been asked before however none of the solutions provided worked.

Examining the token returned from the BeginGetResponse method I see that the following exception is thrown there:

'token.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'

This page tells me that this exception means the Callback parameter is Nothing, however I'm passing the callback - and the debugger breaks into the callback method when I insert a breakpoint. However the request object in the callback is always null. I can view the same exception detail in the result object in the callback method.

I've tried using new AsyncCallback(ProcessResponse) when calling BeginGetResponse

I've tried adding request.AllowReadStreamBuffering = true;

I've tried this in-emulator and on-device, with no luck on either.

public static void GetQuakes(int numDays)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://magma.geonet.org.nz/services/quake/geojson/quake?numberDays=" + numDays);
    // Examining this token reveals the exception.
    var token = request.BeginGetResponse(ProcessResponse, request);
}

static void ProcessResponse(IAsyncResult result)
{
    HttpWebRequest request = result.AsyncState as HttpWebRequest;
    if (request != null)
    {
        // do stuff...
    }
}

So I'm at a bit of a loss as to where to look next.

Community
  • 1
  • 1
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • Bizarre. What wasn't working this morning, now is when I return home. The `if (request != null)` statement was always evaluating as false - ran the exact same code with no edits this evening and it evaluates true. – Adam S Apr 11 '12 at 06:18
  • I am facing this error, but I have no idea how to fix it – Donald Jansen Mar 11 '16 at 11:31

1 Answers1

1

'token.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'

This page tells me that this exception means the Callback parameter is Nothing

The documentation you are looking at http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse%28v=vs.95%29.aspx is for BeginGetResponse. Silverlight does not use the AsyncWaitHandle, and correctly throws a NotSupportedException. You are seeing the exception System.NotSupportedException is for call to IAsyncResult.AsyncWaitHandle you are making when you inspect token.

The documentation on IAsyncResult.AsyncWaitHandle says explicitly that it is up to the implementation of IAsyncResult whether they create a wait handle http://msdn.microsoft.com/en-us/library/system.iasyncresult.asyncwaithandle(v=vs.95).aspx. Worrying about this is sending you down th wrong path.

I think you need to descibe the actual problem you are seeing. It is great to know what you have investigated, but in this case it does help resolve the problem.

The code should work and in ProcessResponse request should not be null when you test it in the if statement. I just copied the code you have provided into a windows phone application and ran it with no problems.

btlog
  • 4,760
  • 2
  • 29
  • 38
  • My problem is that I never get to the 'do stuff' part - in the callback, request is always null. Edit: Ah, after reading the documentation further as you pointed out, I see that that error is expected on inspecting the token. I guess I just jumped on that as the problem. Anyway, the if statement never evaluates to true. – Adam S Apr 11 '12 at 01:42
  • I got home from work just now and the code now appears to work fine - how very strange. Perhaps doing development work before 8am isn't such a great idea. – Adam S Apr 11 '12 at 06:19
  • I am glad that it works. Shows how easy it is to chase the rabbit down the hole when debugging. – btlog Apr 11 '12 at 17:14