在 Silverlight 中捕获 WCF 异常的最佳方法? [英] Best way to catch a WCF exception in Silverlight?

查看:22
本文介绍了在 Silverlight 中捕获 WCF 异常的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 WCF 服务的 Silverlight 2 应用程序.因此,它对所有对服务方法的调用使用异步回调.如果在这些调用之一之前或期间该服务未运行、崩溃或网络出现故障等,则会如您所料生成异常.问题是,我不知道如何捕捉这个异常.

I have a Silverlight 2 application that is consuming a WCF service. As such, it uses asynchronous callbacks for all the calls to the methods of the service. If the service is not running, or it crashes, or the network goes down, etc before or during one of these calls, an exception is generated as you would expect. The problem is, I don't know how to catch this exception.

  • 因为它是一个异步调用,所以我不能用 try/catch 块包装我的 begin 调用,并让它拾取程序从那个点开始移动后发生的异常.

  • Because it is an asynchronous call, I can't wrap my begin call with a try/catch block and have it pick up an exception that happens after the program has moved on from that point.

因为服务代理是自动生成的,所以我不能在每个生成的调用 EndInvoke 的函数上放置 try/catch 块(异常实际出现的地方).这些生成的函数也被调用堆栈中的外部代码包围,因此堆栈中也没有其他地方可以放置 try/catch.

Because the service proxy is automatically generated, I can't put a try/catch block on each and every generated function that calls EndInvoke (where the exception actually shows up). These generated functions are also surrounded by External Code in the call stack, so there's nowhere else in the stack to put a try/catch either.

我无法将 try/catch 放入我的回调函数中,因为异常发生在它们被调用之前.

I can't put the try/catch in my callback functions, because the exception occurs before they would get called.

在我的 App.xaml.cs 中有一个 Application_UnhandledException 函数,它捕获所有未处理的异常.我可以使用它,但这似乎是一种混乱的方法.我宁愿为真正意外的错误(也就是错误)保留这个函数,而不是在我想以特定方式处理的每种情况下在这个函数中使用代码.

There is an Application_UnhandledException function in my App.xaml.cs, which captures all unhandled exceptions. I could use this, but it seems like a messy way to do it. I'd rather reserve this function for the truly unexpected errors (aka bugs) and not end up with code in this function for every circumstance I'd like to deal with in a specific way.

我错过了一个明显的解决方案吗?还是我一直在使用 Application_UnhandledException?

Am I missing an obvious solution? Or am I stuck using Application_UnhandledException?


如下所述,Error 属性正是我要找的.让我陷入循环的原因是异常被抛出并且似乎未被捕获,但执行能够继续.它触发 Application_UnhandledException 事件并导致 VS2008 中断执行,但在调试器中继续允许执行继续.这不是真正的问题,只是看起来很奇怪.


As mentioned below, the Error property is exactly what I was looking for. What is throwing me for a loop is that the fact that the exception is thrown and appears to be uncaught, yet execution is able to continue. It triggers the Application_UnhandledException event and causes VS2008 to break execution, but continuing in the debugger allows execution to continue. It's not really a problem, it just seems odd.

推荐答案

我检查了服务方法已完成事件处理程序中事件参数的 Error 属性.我没有遇到未调用事件处理程序的问题.在服务器出现故障的情况下,调用需要几秒钟,然后在 Error 属性中返回 ProtocolException.

I check the Error property of the event args in the service method completed event handler. I haven't had issues with the event handler not being called. In the case where the server goes down, the call takes a few seconds then comes back with a ProtocolException in the Error property.

假设您已经尝试过这个并且您的回调真的从未被调用,您可能会考虑自定义生成的代理类.请参阅 这篇文章.

Assuming you have tried this and your callback really never gets called, you might look into customizing the generated proxy class. See this article.

这篇关于在 Silverlight 中捕获 WCF 异常的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆