Pergunta

I'm almost finished my first WP7 application and I'd like to publish it to the marketplace. However, one of the stipulations for a published app is that it must not crash unexpectedly during use.

My application almost completely relies on a WCF Azure Service - so I must be connected to the Internet at all times for my functions to work (communicating with a hosted database) - including login, adding/deleting/editing/searching clients and so forth.

When not connected to the internet, or when the connection drops during use, a call to the web service will cause the application to quit. How can I handle this? I figured the failure to connect to the service would be caught and I could handle the exception, but it doesn't work this way.

        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();
        });

EDIT:

My main problem is how to handle the EndpointNotFoundException in WP7 without the application crashing.

Thanks,

Gerard.

Foi útil?

Solução

Your code should look like

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);
}

Your code calls LoginAsync and then immediately CloseAsync, I think this will cause problems...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top