2011-12-12 36 views
0

中的端點異常,我通過後面的代碼動態生成到WCF Web服務的連接。 用戶必須指定Web服務的URL,然後創建我的EndPointAddress和綁定,並且運行良好。 但是,如果用戶輸入無效的網址,則拋出異常:在我的應用程序(Windows Phone)中處理WCF

「System.ServiceModel.EndpointNotFoundException - >沒有端點在[服務的地址]處偵聽,一個不正確的地址或SOAP動作。更多細節見InnerException(如果存在)。「

innerexception非常經典:「遠程服務器返回錯誤:NotException在InnerException。」

問題:我無法處理這個異常。我嘗試了很多在這裏找到的東西,但沒有成功

有沒有解決方法?

我能找到的唯一方法就是在Reference.cs中馴服一些方法,這絕對不是一個好主意......!

預先感謝您,對不起我英文不好:)!

我實際的代碼看起來是這樣,但它不捕獲異常=>

 MyServiceClient client = new MyServiceClient(myBinding, myEndpointAddress); 
     try 
     { 
      client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted); 
      client.RegisterUserAsync(); 
     } 
     catch (TimeoutException ex) 
     { 
      MessageBox.Show(ex.Message); 
      client.Abort(); 
     } 

     catch (FaultException ex) 
     { 
      MessageBox.Show(ex.Message); 
      client.Abort(); 
     } 

     catch (CommunicationException ex) 
     { 
      MessageBox.Show(ex.Message); 
      client.Abort(); 
     } 

     catch 
     { 
      MessageBox.Show("Error"); 
      client.Abort(); 
     } 
     finally 
     { 
      client.CloseAsync(); 
     } 
+0

如何,究竟你要「處理」這個異常?此外,究竟在什麼時候引發異常呢? –

回答

0

您需要將您的嘗試捕獲到client_RegisterUserCompleted方法和周圍的 var x = e.Result;

編輯包:由於我的第一種方法是錯誤的... 首先,您應該首先檢查您的URL(您的代碼中的myEndpointAddress)。您可以在瀏覽器中瀏覽它(您可以在該行上設置斷點,然後複製該值並將其粘貼到瀏覽器中)。

如果這樣的作品,或者沒有顯示出,當你瀏覽到它,然後將它添加到你的主機內的

<system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> <listeners> <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "Trace.svclog" /> </listeners> </source> </sources> </system.diagnostics> 

然後運行你的應用程序的WCF服務的Web應用程序的web.config中的錯誤或瀏覽到它。它將在您的Web應用程序所在的目錄中創建一個Trace.svclog文件。打開該文件並查找紅色條目中突出顯示的內容。這將爲您提供有關該例外的更多信息。 WCF在默認情況下不會返回完整信息以減慢惡意用戶的速度。

+0

感謝您的建議,但我已經試過了。 問題是該方法從未被調用(因爲webservice地址無效),所以RegisterUserCompleted永遠不會到達。 –

2

問題解決了。 解決方案是:uri必須在創建客戶端之前進行測試。 要做到這一點,我做的WebRequest和捕獲異常在WebResponse類:

public void ValidateUri(String uri) 
    { 
     if (!Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute)) return; 
     request = WebRequest.Create(uri); 
     request.BeginGetResponse(new AsyncCallback(ValidateUriCallback), null); 
    } 

    private WebRequest request; 
    private void ValidateUriCallback(IAsyncResult result) 
    { 
     try 
     { 
      WebResponse httpResponse = (WebResponse)request.EndGetResponse(result); 

      // Create the client here 
      ServiceClient client = new MyServiceClient(myBinding, myEndpointAddress); 
     client.RegisterUserCompleted += new EventHandler<RegisterUserCompletedEventArgs>(client_RegisterUserCompleted); 
     client.RegisterUserAsync(); 

     } 
     catch (WebException ex) 
     { 
      var response = ex.Response as System.Net.HttpWebResponse; 
    if (response!=null 
     && response.StatusCode == HttpStatusCode.NotFound) 
    { 
     Dispatcher.BeginInvoke(delegate() 
       { 
        MessageBox.Show("The web service address is not valid", "Sorry", MessageBoxButton.OK); 
       }); 
    } 
     } 
+0

我在Xamarin應用程序中使用了幾年後的這個問題。我在App.cs上創建了一個ValidateUri函數,並在回調函數中傳入 - 這樣我可以在我的應用程序的任何地方使用相同的函數,並根據我需要的服務處理錯誤情況:'public static void TestWebAccess (字符串uri,ref WebRequest請求,Action callBack)' – EvilGeniusJamie

相關問題