CRM Webservice Soap Exception Helper Code

I am currently working through some of the great content created for the Business Action World Tour training sessions, and as part of that, playing with the great Labs that have been created.

I just came across some code that helps to give you a bit more information when you hit a Soap Error when connecting to the CRM Web services. I think its a great way to bubble up that bit more information about what caused the error to the developer (and any error logging you may be doing as well) & help you work out what has happened, so thought I would share it:

 

try

{

//do something here that tries to use the CRM Webservice

}

catch (SoapException ex)

{

string errorMessage = ex.Message;

if(ex.Detail != null)

{

// If there is a InnerText message then we will overwrite with this instead. It can be

// more helpful to explain the error.

errorMessage = ((System.Xml.XmlElement)ex.Detail).InnerText;

}

throw new Exception("Something went wrong when communication with the Web Service. Error: " + errorMessage);

}