Windows phone 7,silverlight - 如何在调用异步 Web 服务时捕获 EndpointNotFoundException? [英] Windows phone 7, silverlight - How to catch EndpointNotFoundException when calling async web service?

查看:15
本文介绍了Windows phone 7,silverlight - 如何在调用异步 Web 服务时捕获 EndpointNotFoundException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Silverlight Windows phone 7 应用程序中调用 Web 服务,如果该 Web 服务可用,该应用程序就可以正常工作.如果 Web 服务不可用,则会捕获未处理的异常,但我自己似乎无法捕获它.我的尝试如下:

I'm calling a web service from within a Silverlight Windows phone 7 application which works fine if the web service is available. If the web service is unavailable then an unhandled exception is caught but I don't seem able to catch it myself. My attempt is as follows:

            //Get list of providers from Webservice
        RSAServiceClient proxy = new RSAServiceClient();
        proxy.GetSuppliersCompleted += new System.EventHandler<GetSuppliersCompletedEventArgs>(ProxyGetSuppliersCompleted);
        try
        {
            proxy.GetSuppliersAsync();
        }
        catch (EndpointNotFoundException)
        {
            //TODO: Handle webserice not being available
        }
        catch (Exception)
        {
            throw;
        }

但这并没有捕获异常,显然 GetSuppliersCompleted 永远不会被调用,所以我无法在那里捕获它.

But this doesn't catch the exception and obviously GetSuppliersCompleted never get's called so I can't catch it there.

然后我认为我可以通过检查连接状态 (proxy.State) 来检测它,但是尽管 Web 服务没有运行,但返回 CommunicationState.Opened.

I then thought I may be able to detect it by checking the connection state (proxy.State) but this despite the web service not running returns CommunicationState.Opened.

知道我可以在哪里处理吗?

Any idea's where I can handle this?

如果我错过了一些东西,但我已经搜索过但没有找到解决方案,我们深表歉意.

Apologies if I've missed something but I have searched and not found a solution.

推荐答案

您应该将您的捕获放在您的 ProxyGetSuppliersCompleted 方法中.

You should place your catch in your ProxyGetSuppliersCompleted method.

尝试访问结果将抛出您期望的错误.

An attempt to access the result will throw the error you are expecting.

或者在 ProxyGetSuppliersCompleted 中,您可以测试将包含异常的 EventArgs 的 Error 属性.

Alternatively in the ProxyGetSuppliersCompleted you can test the Error property of the EventArgs which will contain the exception.

这篇关于Windows phone 7,silverlight - 如何在调用异步 Web 服务时捕获 EndpointNotFoundException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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