Handling errors with the CRM 2011 Web services API


Early on in my experiences with CRM programming I learned to do whatever I can to get meaningful error responses from the server. Nothing is more difficult than having a Web service call fail and not being able to tell what happened by looking at the stack trace.

Since the CRM API is delivered using .NET Web services, we are crossing some process and protocol boundaries. In particular, CRM adheres to the SOAP standard of returning errors as SOAP faults.

In order to implement this feature with .NET exceptions, Microsoft has created a custom exception type – the SoapException. The meaningful data of the SOAP fault is contained in a field called SoapException.Detail.InnerXml. The issue here is that most of the time when the calling code is handling exceptions, we don’t see this field since we are dealing with System.Exception types most of the time.

What I have been doing from the beginning is to rethrow the System.Web.Services.Protocols.SoapException as a System.Exception and include the Xml as the exception message. Consider the following error handler:

try {
	return DoCrmCall();
catch (System.Web.Services.Protocols.SoapException ex) {
	throw new Exception(ex.Detail.InnerXml, ex);

When we see the exception in the calling code (the .NET service proxy) we get a System.Exception where the message contains the Xml from the SOAP fault. There may be better ways of handling the SOAP messages, but this method has been working well for me, and I thought I’d put it up for you guys to comment on, since I know that error messages are a hot button topic in CRM and Web services in general.


No Responses Yet to “Handling errors with the CRM 2011 Web services API”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: