EndpointReference.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 / EndpointReference.cs / 1 / EndpointReference.cs

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

// Define the internal representation of endpoint reference headers 

using System; 
using System.ServiceModel.Channels; 
using System.Collections;
using System.Diagnostics; 
using System.IO;
using System.ServiceModel;
using System.Transactions;
using System.Xml; 

using DiagnosticUtility = Microsoft.Transactions.Bridge.DiagnosticUtility; 
 
namespace Microsoft.Transactions.Wsat.Messaging
{ 
    class EnlistmentHeader : AddressHeader
    {
        public const string HeaderName = DotNetAtomicTransactionExternalStrings.Enlistment;
        public const string HeaderNamespace = DotNetAtomicTransactionExternalStrings.Namespace; 

        Guid enlistment; 
        ControlProtocol protocol; 

        public override string Name 
        {
            get { return HeaderName; }
        }
 
        public override string Namespace
        { 
            get { return HeaderNamespace; } 
        }
 
        public EnlistmentHeader(Guid enlistment, ControlProtocol protocol)
        {
            this.enlistment = enlistment;
            this.protocol = protocol; 
        }
 
        public EnlistmentHeader(Guid enlistment) 
            :
            this(enlistment, ControlProtocol.None) 
        {
        }

        protected override void OnWriteStartAddressHeader (XmlDictionaryWriter writer) 
        {
            writer.WriteStartElement(DotNetAtomicTransactionExternalStrings.Prefix, 
                                     XD.DotNetAtomicTransactionExternalDictionary.Enlistment, 
                                     XD.DotNetAtomicTransactionExternalDictionary.Namespace);
        } 

        protected override void OnWriteAddressHeaderContents (XmlDictionaryWriter writer)
        {
            if (this.protocol != ControlProtocol.None) 
            {
                writer.WriteStartAttribute(XD.DotNetAtomicTransactionExternalDictionary.Protocol, 
                                           XD.DotNetAtomicTransactionExternalDictionary.Namespace); 
                writer.WriteValue((int)this.protocol);
                writer.WriteEndAttribute(); 
            }

            writer.WriteValue(this.enlistment);
        } 

        public static bool ReadFrom (Message message, out Guid enlistmentId, out ControlProtocol protocol) 
        { 
            enlistmentId = Guid.Empty;
            protocol = ControlProtocol.None; 

            int index;
            try
            { 
                index = message.Headers.FindHeader(HeaderName, HeaderNamespace, string.Empty);
            } 
            catch (MessageHeaderException e) 
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); 
                return false;
            }

            if (index >= 0) 
            {
                XmlDictionaryReader reader = message.Headers.GetReaderAtHeader(index); 
                using (reader) 
                {
                    ReadFrom(reader, out enlistmentId, out protocol); 
                }

                MessageHeaderInfo headerInfo = message.Headers[index];
                if (!message.Headers.UnderstoodHeaders.Contains(headerInfo)) 
                {
                    message.Headers.UnderstoodHeaders.Add(headerInfo); 
                } 
                return true;
            } 

            return false;
        }
 
        public static void ReadFrom(XmlDictionaryReader reader,
                                    out Guid enlistment, 
                                    out ControlProtocol protocol) 
        {
            try 
            {
                if (reader.IsEmptyElement ||
                    !reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.Enlistment,
                                           XD.DotNetAtomicTransactionExternalDictionary.Namespace)) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnlistmentHeaderException(SR.GetString(SR.InvalidEnlistmentHeader))); 
                } 

                // Read attribute 
                string protocolAttr = reader.GetAttribute(XD.DotNetAtomicTransactionExternalDictionary.Protocol,
                                                          XD.DotNetAtomicTransactionExternalDictionary.Namespace);

                if (protocolAttr == null) 
                {
                    protocol = ControlProtocol.None; 
                } 
                else
                { 
                    protocol = (ControlProtocol) XmlConvert.ToInt32(protocolAttr.Trim());

                    // If present, must be durable or volatile
                    if (protocol != ControlProtocol.Durable2PC && 
                        protocol != ControlProtocol.Volatile2PC)
                    { 
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnlistmentHeaderException(SR.GetString(SR.InvalidEnlistmentHeader))); 
                    }
                } 

                enlistment = reader.ReadElementContentAsGuid();
            }
            catch (FormatException e) 
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnlistmentHeaderException(SR.GetString(SR.InvalidEnlistmentHeader), e)); 
            }
            catch (OverflowException e) 
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnlistmentHeaderException(SR.GetString(SR.InvalidEnlistmentHeader), e));
            } 
            catch (XmlException e)
            { 
                DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Warning); 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidEnlistmentHeaderException(SR.GetString(SR.InvalidEnlistmentHeader), e));
            } 
        }
    }
}

// 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