我几乎已经完成了我的第一个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/