Library.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / TransactionBridge / Microsoft / Transactions / Wsat / Messaging / Library.cs / 1 / Library.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

// Implement helper methods that wrap ServiceModel functionality 

using System; 
using System.Diagnostics; 
using System.ServiceModel.Channels;
using System.Globalization; 
using System.ServiceModel;
using System.Xml;
using Microsoft.Transactions.Wsat.Protocol;
 
using DiagnosticUtility = Microsoft.Transactions.Bridge.DiagnosticUtility;
 
namespace Microsoft.Transactions.Wsat.Messaging 
{
    static class Library 
    {
        public static Message CreateFaultMessage(UniqueId messageID, MessageVersion messageVersion, Fault fault)
        {
            FaultCode senderCode = FaultCode.CreateSenderFaultCode(fault.Code); 
            MessageFault messageFault = MessageFault.CreateFault(senderCode, fault.Reason);
 
            Message message = Message.CreateMessage(messageVersion, messageFault, messageVersion.Addressing.FaultAction); 

            // Customize our fault message 
            message.Headers.Action = fault.Action;
            if (messageID != null)
            {
                message.Headers.RelatesTo = messageID; 
            }
 
            return message; 
        }
 
        public static void SendFaultResponse(RequestAsyncResult result, Fault fault)
        {
            Message message = Library.CreateFaultMessage(result.MessageId, result.MessageVersion, fault);
            result.Finished(message); 
        }
 
        public static FaultCode GetBaseFaultCode(MessageFault fault) 
        {
            FaultCode code = fault.Code; 
            if (code != null)
            {
                while (code.SubCode != null)
                { 
                    code = code.SubCode;
                } 
            } 
            return code;
        } 

        public static string GetFaultCodeName(MessageFault fault)
        {
            FaultCode code = fault.Code; 
            if (code == null)
            { 
                return "unknown"; 
            }
 
            if (code.SubCode != null)
            {
                code = code.SubCode;
                if (code == null) 
                {
                    return "unknown"; 
                } 
            }
 
            return code.Name;
        }

        public static string GetFaultCodeReason(MessageFault fault) 
        {
            FaultReason reason = fault.Reason; 
            if (reason == null) 
            {
                return "unknown"; 
            }

            FaultReasonText reasonText;
            try 
            {
                reasonText = reason.GetMatchingTranslation(CultureInfo.CurrentCulture); 
            } 
            catch (ArgumentException e)
            { 
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning);
                return "unknown";
            }
 
            return reasonText.Text;
        } 
 
        public static EndpointAddress GetReplyToHeader(MessageHeaders headers)
        { 
            // According to WS-Addressing, the proper search order is ReplyTo, From
            EndpointAddress replyTo = headers.ReplyTo;
            if (replyTo == null || replyTo.IsNone || replyTo.IsAnonymous)
            { 
                replyTo = headers.From;
            } 
 
            return replyTo;
        } 

        public static EndpointAddress GetFaultToHeader(MessageHeaders headers, ProtocolVersion protocolVersion)
        {
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(Library), "GetFaultToHeader"); 
            switch(protocolVersion)
            { 
                case ProtocolVersion.Version10: 
                    return GetFaultToHeader10(headers);
 
                case ProtocolVersion.Version11:
                    return GetFaultToHeader11(headers);

                default: 
                    return null; // inaccessible path because we have asserted the protocol version
            } 
        } 

        static EndpointAddress GetFaultToHeader10(MessageHeaders headers) 
        {
            // According to WS-Addressing, the proper search order is FaultTo, ReplyTo, From
            EndpointAddress faultTo = headers.FaultTo;
            if (faultTo == null) 
            {
                faultTo = headers.ReplyTo; 
                if (faultTo == null) 
                {
                    faultTo = headers.From; 
                }
            }

            return faultTo; 
        }
 
        static EndpointAddress GetFaultToHeader11(MessageHeaders headers) 
        {
            // According to WS-AT 1.1 (06/01/2006) spec we send faults to the 'source endpoint' (From) 
            EndpointAddress sourceAddress = headers.From;
            if(sourceAddress == null || sourceAddress.IsNone || sourceAddress.IsAnonymous)
            {
                return null; 
            }
 
            return sourceAddress; 
        }
 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK