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

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

问题描述

我有一个使用WCF服务的Silverlight 2应用程序。因此,它对所有对该服务的方法的调用使用异步回调。如果服务没有运行,或者在其中一个呼叫之前或之中崩溃,或网络出现故障,则会根据您的预期生成异常。问题是,我不知道如何捕获这个异常。




  • 因为它是一个异步调用,使用try / catch块打包我的开始调用,并在程序从那一点移动之后接收到一个异常。


  • 因为服务代理自动生成,我不能在每个生成的调用EndInvoke的函数(其中异常实际显示)中放置一个try / catch块。这些生成的函数也被调用堆栈中的外部代码包围,所以堆栈中没有其他地方也可以尝试/捕获。


  • 将try / catch放在我的回调函数中,因为异常发生在他们被调用之前。


  • 我的App.xaml中有一个Application_UnhandledException函数。 cs,它捕获所有未处理的异常。我可以使用这个,但它似乎是一个凌乱的方式来做到这一点。我宁愿为这个功能预留这个真正意想不到的错误(也就是错误),而不是最终在这个函数中的代码,我想以一种特定的方式处理的每种情况。




我缺少一个明显的解决方案?或者我使用Application_UnhandledException卡住了?





如下所述,Error属性正是我正在寻找的。什么是扔我一个循环是事实,异常抛出,似乎没有被捕获,但执行能够继续。它触发Application_UnhandledException事件并导致VS2008中断执行,但在调试器中继续允许执行继续。这不是一个真正的问题,只是看起来很奇怪。

解决方案

我检查了服务方法中的事件args的Error属性已完成事件处理程序我没有遇到事件处理程序没有被调用的问题。在服务器关闭的情况下,调用需要几秒钟,然后在Error属性中返回一个ProtocolException。



假设您已经尝试过这个和您的回调永远不会被调用,您可能会查看自定义生成的代理类。请参阅这篇文章


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.

  • 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.

  • 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.

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

  • 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.

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

[Edit]
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.

解决方案

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天全站免登陆