0

非同期呼び出しを同期にする次のコードがあります。

これが悪いことだとは思わないでください。特定の 1 つの要求に対してのみ、この方法で行う必要があります。

using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) が実行されている場合、コードは Windows Phone 8 で例外をスローします。

要求は、実際には UnhandledException イベント ハンドラーで呼び出されます。

例外:NotSupportedException

メッセージ:Method is not supported

スタックトレース:

System.Net.Browser.ClientHttpWebRequest.BeginGetResponse (AsyncCallback コールバック、オブジェクト状態) で Gtas.Core.ServiceRepository.ExecuteGtasRequest (文字列 url、文字列 requestData、ブール型 isError、文字列 filePath) で Gtas.Core.Helpers.GtasRequestWorker.HandleException (例外)例外、AppEnvironment appEnvironment、GtasPerformance GtasPerformance、LimitedCrashExtraDataList extraData、String filePath) で Gtas_WP8.GtasHandler.UnhandledExceptionsHandler(オブジェクト送信者、ApplicationUnhandledExceptionEventArgs e) で MS.Internal.Error.CallApplicationUEHandler(Exception e) で 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());
    }    
}

フレームワークがこれを許可しないことは可能ですか?

前もって感謝します。

4

1 に答える 1

0

EndGetResponse を呼び出す前に、BeginGetResponse が終了するまで待つ必要はありませんか? これにより、NotSupportedException が発生する可能性があります。

さらに、私は同様の問題に遭遇しました。null のコールバックで BeginGetResponse を呼び出そうとすると、NotSupportedException がスローされました。これを定義するだけでこの問題を回避できます

   (a)=>{}

コールバックとして。(シルバーライトでした)

よろしく

クリストフ

于 2013-10-30T11:42:46.320 に答える