NotSupportedException thrown in EndGetResponse(request.BeginGetResponse(null, null)))

710 views Asked by At

I have the following code to make an asynchronous call synchronous.

Please don't get into that this is bad, I know, and it needs to be done this way for only one particular request.

The code throws an exception in Windows Phone 8 when the using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) is executing.

The request is actually invoked in the UnhandledException event handler.

Exception: NotSupportedException

Message: Method is not supported

Stacktrace:

at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state) at Gtas.Core.ServiceRepository.ExecuteGtasRequest(String url, String requestData, Boolean isError, String filePath) at Gtas.Core.Helpers.GtasRequestWorker.HandleException(Exception exception, AppEnvironment appEnvironment, GtasPerformance GtasPerformance, LimitedCrashExtraDataList extraData, String filePath) at Gtas_WP8.GtasHandler.UnhandledExceptionsHandler(Object sender, ApplicationUnhandledExceptionEventArgs e) at MS.Internal.Error.CallApplicationUEHandler(Exception e) at MS.Internal.Error.IsNonRecoverableUserException(Exception ex, UInt32& xresultValue)

public void ExecuteRequest(string url, string requestData)
{
    try
    {
        WebRequest request = WebRequest.Create(new Uri(url));
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Headers["Header-Key"] = "AKey";

        // Convert the string into a byte array.
        byte[] postBytes = Encoding.UTF8.GetBytes(requestData);

        using (var requestStream = request.EndGetRequestStream(request.BeginGetRequestStream(null, null)))
        {
            // Write to the request stream.
            endGetRequestStream.Write(postBytes, 0, postBytes.Length);
        }

        using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) // NotSupportedException
        {
            using (var streamRead = new StreamReader(response.GetResponseStream()))
            {
                // The Response
                string responseString = streamRead.ReadToEnd();

                if (!string.IsNullOrWhiteSpace(requestDataObjResult.FileName))
                {
                    var fileRepo = new FileRepository();
                    fileRepo.Delete(request.FileName);
                }

                Debug.WriteLine("Response : {0}", responseString);
            }
        }
    }
    catch (WebException webEx)
    {
        WebExceptionStatus status = webEx.Status;
        WebResponse responseEx = webEx.Response;
        Debug.WriteLine(webEx.ToString());
    }    
}

Is it possible for the framework not to allow this?

Thank you in advance.

1

There are 1 answers

0
christoph On

donĀ“t you need to wait until BeginGetResponse is finished before you call EndGetResponse? That could cause the NotSupportedException.

Further I came along a similar problem. There was a NotSupportedException thrown when I tried to call BeginGetResponse with a callback of null. I get around this issue by just defining this

   (a)=>{}

as the callback. (That was silverlight)

Regards

Christoph