Monday, 20 June 2011

Handling WCF Exceptions from ASPX page

If a WCF method fails when you call it from your ASPX page you might want to save the exception details somewhere, in a database table or in a text/xml file, or output it in the browser. But how will you get the details of the WCF exception in your ASP.NET code-behind class? You may find the following code snippet useful.


string serviceStackTrace = "";

try
{
//WCF method call
}
catch(Exception ex)
{
if (ex is FaultException)
serviceStackTrace = ((System.ServiceModel.FaultException)(ex)).Detail.StackTrace;
else if (ex is System.ServiceModel.CommunicationException)
serviceStackTrace = ex.StackTrace;
}

//Do whatever you want to do with serviceStackTrace

No comments:

Post a Comment