2013-04-05 66 views
0

這個問題真的很奇怪,並且無法調試我的嘗試。它僅在Surface平板電腦上運行應用程序時纔會發生。它不會在華碩平板電腦上運行,也不會在Visual Studio中運行。在飛行模式已打開的特定情況下,拋出WebException,我絕對無法捕捉。我甚至不完全確定在我的代碼中是什麼導致它發生的,因爲我的一些日誌記錄在代碼中的某個點之後沒有發生,原因不明。我只能假設它是由HttpWebRequest引起的,因爲拋出異常的類型,這似乎來自內部的.NET組件。這是我能夠獲得的唯一調試信息。這是從Windows事件查看器:在WinRT應用程序中拋出的WebException不能被捕獲

Application: <myappname.exe> 
Framework Version: v4.0.30319 
Description: The process was terminated due to an unhandled exception. 
Exception Info: System.Net.WebException 
Stack: 
    at System.Net.ServicePoint.ConnectSocketCallback(System.IAsyncResult) 
    at System.Net.LazyAsyncResult.Complete(IntPtr) 
    at System.Net.ContextAwareResult.Complete(IntPtr) 
    at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr) 
    at System.Net.Sockets.Socket.ConnectCallback() 
    at System.Net.Sockets.Socket.RegisteredWaitCallback(System.Object, Boolean) 
    at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(System.Object, Boolean) 

我真希望我有更多的調試信息提供,但我試過最calls--後一切我能想到的與噸try/catch塊隨處可見的和記錄其中一些不被執行。有沒有人有任何線索可能導致這種情況?

編輯1:

基於痕跡異常似乎在這裏的某個地方拋出。幾乎所有東西都被包裝在一個try/catch塊中,所以我看不到WebException可能如何滑落。

byte[] bytes = Encoding.UTF8.GetBytes(requestXml.ToString()); 
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
request.Method = "POST"; 
request.ContentType = "text/xml"; 

try 
{ 
    IAsyncResult requestResult = (IAsyncResult)request.BeginGetRequestStream((rAsyncResult) => 
    { 
     using (Stream uploadStream = request.EndGetRequestStream(rAsyncResult)) 
     { 
      try 
      { 
       uploadStream.Write(bytes, 0, bytes.Length); 
       uploadStream.Flush(); 
      } 
      catch (Exception e) 
      { 
       // Exception handling 
      } 
      finally 
      { 
       uploadStream.Dispose(); 
      } 
     } 

     IAsyncResult responseResult = (IAsyncResult)request.BeginGetResponse((resAsyncResult) => 
     { 
      try 
      { 
       using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(resAsyncResult)) 
       { 
        try 
        { 
         data = ProcessResponse(XmlReader.Create(response.GetResponseStream())); 
        } 
        catch (Exception e) 
        { 
         // Exception handling 
        } 
        finally 
        { 
         response.Dispose(); 
        } 
       } 
      } 
      catch (WebException e) 
      { 
       // Exception handling 
      } 

     }, null); 
    }, null); 
} 
catch (Exception e) 
{ 
    // Exception handling 
} 

編輯2:

我還沒有找到一個可接受的解決方案。我目前正在檢查連接類型,如果它未連接到WiFi,移動或以太網,則不允許代碼繼續運行,但不能保證代碼連接到沒有Internet連接的網絡。 WinRT無法檢查互聯網連接,甚至我使用的方法也不友好,只能傳回一個數字 - http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/d8e76732-19d3-47b3-840f-70d87c75ce9f

+1

也許顯示你的代碼將有助於..,如果有一個例外是有辦法抓住它。也許你試圖抓住它錯誤地.. – MethodMan 2013-04-05 16:33:32

+0

@DJ KRAZE發佈了我認爲引發異常的代碼。 – jokeefe 2013-04-08 15:27:13

+0

這真的是你的'異常代碼塊'看起來如何..?您沒有處理任何異常 – MethodMan 2013-04-08 15:45:22

回答

0

您是否嘗試過處理Application.UnhandledException

事件處理程序添加到事件中App類的構造函數:

public App() 
{ 
    // keep the rest of the constructor 
    this.UnhandledException += OnUnhandledException; 
} 

在事件處理程序,你可以記錄異常和標記處理,以防止關閉/崩潰的應用程序:

void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    // log e.Exception details for investigation 
    e.Handled = true; 
} 
+0

這是我嘗試的第一件事,但謝謝你的建議。在WinRT平臺上,這顯然只適用於在主線程上拋出的異常,這發生在另一個線程中。這是「已知限制:」http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/bea154b0-08b0-4fdc-be31-058d9f5d1c4e。它使我認爲功能相對無用。謝謝,不過。 – jokeefe 2013-04-05 20:53:08

0

該問題可能是由於回調中引發了未處理的異常;回調在異步執行的線程不同於調用最初的request.BeginGetRequestStream的線程的機會很高,這就是爲什麼你沒有捕獲它在外部try/catch塊中的原因。 您應該能夠通過在try/catch塊包裹回調的全部內容,以解決這個問題,那就是:

IAsyncResult requestResult = (IAsyncResult)request.BeginGetRequestStream((rAsyncResult) => 
{ 
    try 
    { 
     using (Stream uploadStream = request.EndGetRequestStream(rAsyncResult)) 
     { 
      try 
      { 
       uploadStream.Write(bytes, 0, bytes.Length); 
       uploadStream.Flush(); 
      } 
      catch (Exception e) 
      { 
       // Exception handling 
      } 
      finally 
      { 
       uploadStream.Dispose(); 
      } 
     } 

     IAsyncResult responseResult = (IAsyncResult)request.BeginGetResponse((resAsyncResult) => 
     { 
      try 
      { 
       using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(resAsyncResult)) 
       { 
        try 
        { 
         data = ProcessResponse(XmlReader.Create(response.GetResponseStream())); 
        } 
        catch (Exception e) 
        { 
         // Exception handling 
        } 
        finally 
        { 
         response.Dispose(); 
        } 
       } 
      } 
      catch (WebException e) 
      { 
       // Exception handling 
      } 

     }, null); 
    } 
    catch (Exception ex) 
    { 
     // Handle the exception as you wish 
    } 
}, null); 
0

像Efran Cobisi說EndGetRequestStream可能引發異常的功能。即使有異常,using語句也會處理對象,因此不需要嘗試最終處理它。 但是在任何情況下,您都應該使用異步方法,這會使代碼更容易被讀取,並且更易讀。你的代碼相當於將是:

byte[] bytes = Encoding.UTF8.GetBytes(requestXml.ToString()); 
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
request.Method = "POST"; 
request.ContentType = "text/xml"; 

try 
{ 

    using (Stream uploadStream = await request.GetRequestStreamAsync()) 
    { 
     await uploadStream.WriteAsync(bytes, 0, bytes.Length); 
     await uploadStream.FlushAsync(); 
    } 

    using (HttpWebResponse response = (HttpWebResponse) await request.GetRequestStreamAsync()) 
    { 
     data = ProcessResponse(XmlReader.Create(response.GetResponseStream())); 
    } 

} 
catch (Exception e) 
{ 
    // Exception 
} 
相關問題