wcf - 处理需要Web服务的应用程序-处理EndpointNotFoundExceptions

标签 wcf windows-phone-7 crash connection marketplace

我几乎已经完成了我的第一个WP7应用程序,并希望将其发布到市场上。但是,发布的应用程序的其中一项规定是,在使用过程中不得崩溃。

我的应用程序几乎完全依赖于WCF Azure服务-因此我必须始终连接到Internet才能使我的功能正常工作(与托管数据库进行通信)-包括登录,添加/删除/编辑/搜索客户端等。

当未连接到Internet或使用过程中连接断开时,对Web服务的调用将导致应用程序退出。我该如何处理?我认为连接到服务的失败将得到解决,我可以处理该异常,但是这种方式无法正常工作。

        LoginCommand = new RelayCommand(() =>
        {
            ApplicationBarHelper.UpdateBindingOnFocussedControl();
            MyTrainerReference.MyTrainerServiceClient service = new MyTrainerReference.MyTrainerServiceClient();

            // get list of clients from web service
            service.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(service_LoginCompleted);

            try
            {
                service.LoginAsync(Email, Password);
            }
            **catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }**
            service.CloseAsync();
        });

编辑:

我的主要问题是如何在WP7中处理EndpointNotFoundException而不会导致应用程序崩溃。

谢谢,

杰拉德。

最佳答案

您的代码应如下所示

LoginCommand = new RelayCommand(Login);
...

public void Login()
{
    var svc = new MyTrainerReference.MyTrainerServiceClient();
    try
    {
        svc.LoginCompleted += LoginCompleted;
        svc.LoginAsync();
    }
    catch (Exception e)
    {
        svc.CloseAsync();
        ShowError(e);
    }
}

private void LoginCompleted(object sender, LoginCompletedEventArgs e)
{
    ((MyTrainerReference.MyTrainerServiceClient)sender).LoginCompleted -= LoginCompleted;
    ((MyTrainerReference.MyTrainerServiceClient)sender).CloseAsync();

    if (e.Error == null && !e.Cancelled)
    {
        // TODO process e.Result
    }
    else if (!e.Cancelled)
    {
        ShowError(e.Error);
    }
}

private void ShowError(Exception e)
{
    // TODO show error
    MessageBox.Show(e.Message, "An error occured", MessageBoxButton.OK);
}

您的代码先调用LoginAsync,然后立即调用CloseAsync,我认为这会导致问题...

关于wcf - 处理需要Web服务的应用程序-处理EndpointNotFoundExceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079875/

相关文章:

iphone - 应用程序崩溃,但仅在为 Ad-Hoc 发行版构建时

javascript - android 无法看到网络服务

c# - 使用 HTTP GET 配置 WCF 客户端以使用 WCF 服务

javascript - 如何使用 SSL 从 extjs 调用 rest

c# - 将在线 XML 文件中的条目写入隔离存储

android - 连续向上/向下滚动 fragment 时崩溃

c# - 如何手动创建 Silverlight PollingDuplex 客户端/代理?

c# - 将来自 Facebook API 的字符串放入列表框

c# - 为 WebClient 请求设置 User-Agent header

iphone - 触摸状态栏时崩溃