gpt4 book ai didi

c# - 在哪里捕获异步 WCF 调用的 EndpointNotFoundException?

转载 作者:行者123 更新时间:2023-11-30 12:36:07 25 4
gpt4 key购买 nike

我在测试期间遇到异常捕获时遇到了一些困难。我实际上断开了服务,因此端点不可用,并且我正在尝试修改我的应用程序以处理这种可能性。

问题是无论我把 try/catch block 放在哪里,我似乎都无法在它未处理之前捕捉到它。

我已经尝试将我的创建代码包装在 try/catch 中,

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

以及服务调用完成时调用的委托(delegate)。

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

没有骰子。有什么想法吗?

最佳答案

我建议您使用 IAsyncResult。当您在客户端生成 WCF 代理并获取异步调用时,您应该获取 TopicServiceClient.BeginGetAllTopics() 方法。此方法返回一个 IAsyncResult 对象。它还需要一个 AsyncCallback 委托(delegate)在完成时调用。完成后,您调用 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"/>

关于c# - 在哪里捕获异步 WCF 调用的 EndpointNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3919143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com