2010-10-12 46 views
4

我有一些困難,我在測試過程中捕捉異常。我實際上斷開了該服務,使端點不可用,並且我試圖修改我的應用程序以處理這種可能性。我在哪裏可以捕獲異步WCF調用的EndpointNotFoundException?

的問題是,無論在哪裏我把try/catch塊,我似乎無法趕上這個東西它到達之前未處理。

我已經試過包裝在try/catch語句我的兩個創建代碼,

this.TopicServiceClient = new KeepTalkingServiceReference.TopicServiceClient(); 
      this.TopicServiceClient.GetAllTopicsCompleted += new EventHandler<KeepTalkingServiceReference.GetAllTopicsCompletedEventArgs>(TopicServiceClient_GetAllTopicsCompleted); 
      this.TopicServiceClient.GetAllTopicsAsync(); 

以及服務調用完成時調用的委託。

public void TopicServiceClient_GetAllTopicsCompleted(object sender, KeepTalkingServiceReference.GetAllTopicsCompletedEventArgs e) 
     { 
      try 
      { 
... 

沒有骰子。有什麼想法嗎?

回答

2

我會建議您使用的IAsyncResult。當您生成客戶端上的WCF代理,並獲得異步調用,你應該得到一個TopicServiceClient.BeginGetAllTopics()方法。此方法返回一個IAsyncResult對象。它還需要一個AsyncCallback委託在完成時調用。當它完成,你叫EndGetAllTopics()供應的IAsyncResult(傳遞給EndGetAllTopics()。

你把一個try/catch圍繞您的來電BeginGetAllTopics(),並且應該抓住你後例外。如果發生異常遠程地,也就是說,你確實連接了,但是服務會拋出一個異常,在你調用EndGetAllTopics()的地方處理。

這是一個非常簡單的例子(顯然不是生產)這是用WCF 4.0編寫的

namespace WcfClient 
{ 
class Program 
{ 
    static IAsyncResult ar; 
    static Service1Client client; 
    static void Main(string[] args) 
    { 
     client = new Service1Client(); 
     try 
     { 
      ar = client.BeginGetData(2, new AsyncCallback(myCallback), null); 
      ar.AsyncWaitHandle.WaitOne(); 
      ar = client.BeginGetDataUsingDataContract(null, new AsyncCallback(myCallbackContract), null); 
      ar.AsyncWaitHandle.WaitOne(); 
     } 
     catch (Exception ex1) 
     { 
      Console.WriteLine("{0}", ex1.Message); 
     } 
     Console.ReadLine(); 
    } 
    static void myCallback(IAsyncResult arDone) 
    { 
     Console.WriteLine("{0}", client.EndGetData(arDone)); 
    } 
    static void myCallbackContract(IAsyncResult arDone) 
    { 
     try 
     { 
      Console.WriteLine("{0}", client.EndGetDataUsingDataContract(arDone).ToString()); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0}", ex.Message); 
     } 
    } 
} 
} 

對於服務器端異常傳播ba CK到客戶端,則需要設置服務器web配置如下......

<serviceDebug includeExceptionDetailInFaults="true"/> 
相關問題