C# 在哪里捕获异步WCF调用的EndpointNotFoundException?
我在测试期间捕获异常时遇到了一些困难。实际上,我断开了服务的连接,因此端点不可用,我正在尝试修改我的应用程序以处理这种可能性 问题是,无论我把try/catch块放在哪里,我似乎都无法在它未被处理之前捕捉到它 我已尝试在try/catch中包装我的两个创建代码C# 在哪里捕获异步WCF调用的EndpointNotFoundException?,c#,silverlight,wcf,exception-handling,windows-phone-7,C#,Silverlight,Wcf,Exception Handling,Windows Phone 7,我在测试期间捕获异常时遇到了一些困难。实际上,我断开了服务的连接,因此端点不可用,我正在尝试修改我的应用程序以处理这种可能性 问题是,无论我把try/catch块放在哪里,我似乎都无法在它未被处理之前捕捉到它 我已尝试在try/catch中包装我的两个创建代码 this.TopicServiceClient = new KeepTalkingServiceReference.TopicServiceClient(); this.TopicServiceClient.Get
this.TopicServiceClient = new KeepTalkingServiceReference.TopicServiceClient();
this.TopicServiceClient.GetAllTopicsCompleted += new EventHandler<KeepTalkingServiceReference.GetAllTopicsCompletedEventArgs>(TopicServiceClient_GetAllTopicsCompleted);
this.TopicServiceClient.GetAllTopicsAsync();
没有骰子。有什么想法吗?我建议您使用iSyncResult。在客户端上生成WCF代理并获取异步调用时,应该获取TopicServiceClient.BeginGetAllTopics()方法。此方法返回一个IAsyncResult对象。它还需要在完成时调用AsyncCallback委托。完成后,调用EndGetAllTopics(),提供IASyncResult(传递给EndGetAllTopics()) 您在对BeginGetAllTopics()的调用周围放置了一个try/catch,它应该捕获您所关注的异常。如果异常发生在远程,即您确实连接了,但服务抛出了一个异常,该异常将在您调用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);
}
}
}
}
要使服务器端异常传播回客户端,需要在服务器web配置中设置以下内容
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>