Error Handling on the Client
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
This topic describes how you typically want to handle errors and take certain steps in response to the errors that you retrieve or modify data from a client. With Open Ria Services, you handle errors by providing a callback method for data operations and checking for errors in that callback method. Using callback methods is necessary because calls to data operations are asynchronous and therefore any exceptions are thrown asynchronously. By default, an exception is thrown for any errors in domain operations. Open Ria Services provides ways for you to handle the errors and specify that the framework not throw an exception.

Error Handling When Loading Data

When loading data from a query method, you can either handle the error or choose to ignore the error. Specifically, you choose from the following options:
  • Use a Load method that has a parameter for a callback method. In the callback method, handle the error and call the MarkErrorAsHandled method to indicate that the exception is not thrown.
  • Use a Load method that has a boolean parameter named throwOnError. Set throwOnError to false when you call the Load method to indicate that you do not want an exception thrown for query errors.
  • Use the Load method which does not have a parameter for a callback method or a boolean parameter. Any errors when running the query will result in an unhandled exception.
The following example shows how to load data from a query and specify a callback method that checks for errors from the load operation.
1
Private _customerContext As New CustomerDomainContext
2
​
3
Public Sub New()
4
InitializeComponent()
5
​
6
Dim loadOp = Me._customerContext.Load(Me._customerContext.GetCustomersQuery(), AddressOf OnLoadCompleted, Nothing)
7
CustomerGrid.ItemsSource = loadOp.Entities
8
End Sub
9
​
10
Private Sub OnLoadCompleted(ByVal lo As LoadOperation(Of Customer))
11
If (lo.HasError) Then
12
MessageBox.Show(String.Format("Retrieving data failed: {0}", lo.Error.Message))
13
lo.MarkErrorAsHandled()
14
End If
15
End Sub
Copied!
1
private CustomerDomainContext _customerContext = new CustomerDomainContext();
2
​
3
public MainPage()
4
{
5
InitializeComponent();
6
​
7
LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery(), OnLoadCompleted, null);
8
CustomerGrid.ItemsSource = loadOp.Entities;
9
}
10
​
11
private void OnLoadCompleted(LoadOperation<Customer> lo)
12
{
13
if (lo.HasError)
14
{
15
MessageBox.Show(string.Format("Retrieving data failed: {0}", lo.Error.Message));
16
lo.MarkErrorAsHandled();
17
}
18
}
Copied!

Error Handling When Submitting Data

When submitting data, you cannot choose to turn off the exceptions as you can with the Load method. Any errors when submitting data will result in an exception. Specifically, you choose from the following options:
  • Use the SubmitChanges method and provide a callback method as a parameter. In the callback method, handle the error and call the MarkErrorAsHandled method to indicate that the exception is not thrown.
  • Use the SubmitChanges method. Any errors when submitting the data will result in an unhandled exception.
The following example shows how to call the SubmitChanges method with a callback method for handling errors.
1
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
2
_customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
3
End Sub
4
​
5
Private Sub RejectButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
6
_customerContext.RejectChanges()
7
CheckChanges()
8
End Sub
9
​
10
Private Sub CustomerGrid_RowEditEnded(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridRowEditEndedEventArgs)
11
CheckChanges()
12
End Sub
13
​
14
Private Sub CheckChanges()
15
Dim changeSet = _customerContext.EntityContainer.GetChanges()
16
ChangeText.Text = changeSet.ToString()
17
​
18
Dim hasChanges = _customerContext.HasChanges
19
SaveButton.IsEnabled = hasChanges
20
RejectButton.IsEnabled = hasChanges
21
End Sub
22
​
23
Private Sub OnSubmitCompleted(ByVal so As SubmitOperation)
24
If (so.HasError) Then
25
MessageBox.Show(String.Format("Submit Failed: {0}", so.Error.Message))
26
so.MarkErrorAsHandled()
27
End If
28
CheckChanges()
29
End Sub
Copied!
1
private void SaveButton_Click(object sender, RoutedEventArgs e)
2
{
3
_customerContext.SubmitChanges(OnSubmitCompleted, null);
4
}
5
​
6
private void RejectButton_Click(object sender, RoutedEventArgs e)
7
{
8
_customerContext.RejectChanges();
9
CheckChanges();
10
}
11
​
12
private void CustomerGrid_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
13
{
14
CheckChanges();
15
}
16
​
17
private void CheckChanges()
18
{
19
EntityChangeSet changeSet = _customerContext.EntityContainer.GetChanges();
20
ChangeText.Text = changeSet.ToString();
21
​
22
bool hasChanges = _customerContext.HasChanges;
23
SaveButton.IsEnabled = hasChanges;
24
RejectButton.IsEnabled = hasChanges;
25
}
26
​
27
private void OnSubmitCompleted(SubmitOperation so)
28
{
29
if (so.HasError)
30
{
31
MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
32
so.MarkErrorAsHandled();
33
}
34
CheckChanges();
35
}
Copied!

Error Handling With Invoke Operations

When invoking an operation, you have the same options available as when you submit data. Specifically, you choose from the following options:
  • Include a callback method when you call the invoke operation. In the callback method, handle the error and call the MarkErrorAsHandled method to indicate that the exception is not thrown.
  • Call the invoke operation without including a callback method. Any errors when invoking the method will result in an unhandled exception.
The following example shows an invoke operation with a callback method.
1
Dim invokeOp As InvokeOperation(Of Integer)
2
invokeOp = customerContext.GetLocalTemperature(selectedPostalCode, AddressOf OnInvokeCompleted, Nothing)
3
​
4
Private Sub OnInvokeCompleted(ByVal invOp As InvokeOperation(Of Integer))
5
If (invOp.HasError) Then
6
MessageBox.Show(String.Format("Method Failed: {0}", invOp.Error.Message))
7
invOp.MarkErrorAsHandled()
8
Else
9
result = invOp.Value
10
End If
11
End Sub
Copied!
1
InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode, OnInvokeCompleted, null);
2
​
3
private void OnInvokeCompleted(InvokeOperation<int> invOp)
4
{
5
if (invOp.HasError)
6
{
7
MessageBox.Show(string.Format("Method Failed: {0}", invOp.Error.Message));
8
invOp.MarkErrorAsHandled();
9
}
10
else
11
{
12
result = invokeOp.Value;
13
}
14
}
Copied!

Error Handling with Authentication Service

The AuthenticationService class enables you to provide a callback method when you call the following methods:
In the callback method, you can provide code to handle errors from the authentication service. The following example shows how to call the Login method from an event handler for a login button. A callback method is included to respond to the results of the login operation.
1
Private Sub LoginButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
2
Dim lp As LoginParameters = New LoginParameters(UserName.Text, Password.Password)
3
WebContext.Current.Authentication.Login(lp, AddressOf Me.LoginOperation_Completed, Nothing)
4
LoginButton.IsEnabled = False
5
LoginResult.Text = ""
6
End Sub
7
​
8
Private Sub LoginOperation_Completed(ByVal lo As LoginOperation)
9
If (lo.HasError) Then
10
LoginResult.Text = lo.Error.Message
11
LoginResult.Visibility = System.Windows.Visibility.Visible
12
lo.MarkErrorAsHandled()
13
ElseIf (lo.LoginSuccess = False) Then
14
LoginResult.Text = "Login failed. Please check user name and password."
15
LoginResult.Visibility = System.Windows.Visibility.Visible
16
ElseIf (lo.LoginSuccess = True) Then
17
SetControlVisibility(True)
18
End If
19
LoginButton.IsEnabled = True
20
End Sub
Copied!
1
private void LoginButton_Click(object sender, RoutedEventArgs e)
2
{
3
LoginParameters lp = new LoginParameters(UserName.Text, Password.Password);
4
WebContext.Current.Authentication.Login(lp, this.LoginOperation_Completed, null);
5
LoginButton.IsEnabled = false;
6
LoginResult.Text = "";
7
}
8
​
9
private void LoginOperation_Completed(LoginOperation lo)
10
{
11
if (lo.HasError)
12
{
13
LoginResult.Text = lo.Error.Message;
14
LoginResult.Visibility = System.Windows.Visibility.Visible;
15
lo.MarkErrorAsHandled();
16
}
17
else if (lo.LoginSuccess == false)
18
{
19
LoginResult.Text = "Login failed. Please check user name and password.";
20
LoginResult.Visibility = System.Windows.Visibility.Visible;
21
}
22
else if (lo.LoginSuccess == true)
23
{
24
SetControlVisibility(true);
25
}
26
LoginButton.IsEnabled = true;
27
}
Copied!
Last modified 6mo ago