WCF RIA Services Exception Handling

Note: The examples in this blog post are based on the WCF RIA Services PDC beta, and changes to the framework can be done until it hits RTM.

In a preview blog post I wrote about how to handle exception when using .NET RIA Services and the Load method. This blog post will be about the same but instead based on the WCF RIA Services.

If you want add a generic way to log exceptions thrown on the server-side, you can override the DomainService OnError method:

[EnableClientAccess()]
public class DomainService1 : DomainService
{
public IEnumerable<Customer> GetCustomers()
{
throw new ApplicationException("My exception");
}

protected override void OnError(DomainServiceErrorInfo errorInfo)
{
//Log exception errorInfo.Error
}
}

When you make a call to the Load method of the DomainContext on the client-side and the Load operation will fail, an  exception will be thrown when the Load operation is completed. If you use Silverlight as the client and you don’t handle the exception on the client-side, the App’s Application_UnhandledException will be executed. This is new to WCF RIA Services In .NET RIA Services no exception was thrown on the client-side.

Something to be aware of is that the WCR RIA Services will use the customErrors section in the web.config to pass a detail server-side exception to the client, or not.

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm"/>

If customErrors is on or remoteOnly (and you aren’t running the app locally on the remote machine), the message of the  exception throw on the server-side will not be passed to the client. You will still get an exception, but the information you will get is the name of the server-side method that throw an exception “Load operation failed for query ‘GetCustomers’. …..”. The type of the exception is System.Windows.Ria.DomainOperationException. You will get the same exception type even if the customErrors mode is set to Off or remoteOnly (When you are running the app locally on the remote machine), but after the name of the method, you will also get the server-side exception message “Load operation failed for query ‘GetCustomers’: My exception”.

NOTE: Don’t include sensitive information in the exception message that can be used by a hacker, so think through what kind of message you want to send to the client. In most cases a simple message like “Retrieving customers failed, please try again, if you see the same message please contact an administrator”. Make sure you log the original message so you have something to analyze if a user will contact you.

There are several of ways to check if a server-side load operation failed when using the DomainService Load method. You can either use the LoadOperation object returned from the Load method, and hook up to its completed event, or pass in a callback to the Load method. I prefer to use a callback. Here is an example where an MessageBox will show an exception message if the GetCustomers method fails:

customerDomainContext.Load<Customer>(ds.GetCustomersQuery(),
loadOperation =>
{
if (loadOperation.HasError)
{
MessageBox.Show(loadOperation.Error.Message);
loadOperation.MarkErrorAsHandled();
}
}
,null);

The LoadOperation has a HasError property, you can use this property to see if the load operation has failed. You can then use the Error property of the LoadOperation to get the error message from the server-side (Remember the customErrors mentioned earlier, it can prevent you from getting the message throw from the server-side). By using the LoadOperation’s MarkErrorAsHandled method, you will tell the WCR RIA Services that you have handled the exception, no reason for passing it along. There is also property which you can use to see or specify that the exception is handled, and the property is IsErrorHandled.

If you want to know when I publish a new blog post, you can follow me on twitter: http://www.twitter.com/fredrikn

Published Tuesday, December 8, 2009 8:20 PM by Fredrik N

Comments

# re: WCF RIA Services Exception Handling

Wednesday, December 9, 2009 12:19 PM by alexy

Thanks Fredrik for this post.

Looking forward to hear more from you about WCR RIA stuff.

For example, recently one of our WCR RIA services fired the exception about timeout of the submit operation.

Is there any way you can control  this? I know how to do it for a regular WCF thru web.config.

Even if WCF RIA service looks like a regular WCF I don't see the place where you can configure its options.

Alex Y

# re: WCF RIA Services Exception Handling

Wednesday, December 16, 2009 9:44 AM by Gaurave Sehgal

Nice article. overriding onError surely helped me a lot.

Leave a Comment

(required) 
(required) 
(optional)
(required)