這個問題真的很奇怪,並且無法調試我的嘗試。它僅在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。
也許顯示你的代碼將有助於..,如果有一個例外是有辦法抓住它。也許你試圖抓住它錯誤地.. – MethodMan 2013-04-05 16:33:32
@DJ KRAZE發佈了我認爲引發異常的代碼。 – jokeefe 2013-04-08 15:27:13
這真的是你的'異常代碼塊'看起來如何..?您沒有處理任何異常 – MethodMan 2013-04-08 15:45:22