2010-10-12 41 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"/> 
相关问题