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.

Private _customerContext As New CustomerDomainContext

Public Sub New()
    InitializeComponent()

    Dim loadOp = Me._customerContext.Load(Me._customerContext.GetCustomersQuery(), AddressOf OnLoadCompleted, Nothing)
    CustomerGrid.ItemsSource = loadOp.Entities
End Sub

Private Sub OnLoadCompleted(ByVal lo As LoadOperation(Of Customer))
    If (lo.HasError) Then
        MessageBox.Show(String.Format("Retrieving data failed: {0}", lo.Error.Message))
        lo.MarkErrorAsHandled()
    End If
End Sub

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.

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.

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.

Last updated

Was this helpful?