Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Catch and Handle WCF Service Exceptions in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.94/5 (18 votes)
22 Jan 2009CPOL12 min read 105.5K   1.1K   52  
A reusable set of code to enable service thrown exception handling in Silverlight.
using System.ServiceModel;
using System.ServiceModel.Channels;
using WCFHelpers.Client;

namespace WCFHelpers
{
    public static class Global
    {
        public enum FaultHandling
        {
            /// <summary>
            /// The client cannot handle faults Fault handling 
            /// </summary>
            None,
            Simple,
            FullContract,
        }

        public const string FaultHandlingNS = "urn:Silverlight-FaultHandler";

        /// <summary>
        /// Adds a message header to the current operation stating that the client does not support standard Fault contracting
        /// </summary>
        /// <param name="Context">The active <see cref="OperationContext"/></param>
        public static void SetNoFaultHandling(OperationContext Context)
        {
            //Remove any previous Header
            int iHeader = Context.OutgoingMessageHeaders.FindHeader("NoFaultHandling", FaultHandlingNS);
            if (iHeader >= 0)
                Context.OutgoingMessageHeaders.RemoveAt(iHeader);

            Context.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("NoFaultHandling", FaultHandlingNS, true));

        }

        /// <summary>
        /// Checks the current operation for a message header stating if it can handle standard fault contracts
        /// </summary>
        /// <param name="Context">The active <see cref="OperationContext"/></param>
        /// <returns>false if the current operation has explicitly stated it cannot handle faults, true otherwise</returns>
        public static bool HandlesFaults(OperationContext Context)
        {
            int iHeader = Context.IncomingMessageHeaders.FindHeader("NoFaultHandling", FaultHandlingNS);
            if (iHeader < 0)
                return true;
            return !Context.IncomingMessageHeaders.GetHeader<bool>(iHeader);
        }

        /// <summary>
        /// Adds <see cref="FaultDetail"/> to the current Outgoing message headers
        /// </summary>
        /// <param name="Fault">The <see cref="FaultDetail"/> to add to the message</param>
        /// <param name="Context">the active <see cref="OperationContext"/></param>
        public static void SetFault(FaultDetail Fault, OperationContext Context)
        {
            Context.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Fault", FaultHandlingNS, Fault));
        }
        /// <summary>
        /// Extracts and returns a <see cref="FaultDetail"/> object from the message headers if present
        /// </summary>
        /// <param name="Context">The active <see cref="OperationContext"/></param>
        /// <returns>A <see cref="FaultDetail"/> if it was present in the returning message</returns>
        public static FaultDetail GetFault(OperationContext Context)
        {
            int iHeader = Context.IncomingMessageHeaders.FindHeader("Fault", FaultHandlingNS);
            if (iHeader >= 0)
                return Context.IncomingMessageHeaders.GetHeader<FaultDetail>(iHeader);
            return null;
        }
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) VCM Software
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions