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



我几乎完成了我的第一个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,我认为这会引起问题...

最新更新