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

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

// Implementation for all WS-Coor, WS-AT and .NET-AT messages 

using System; 
using System.ServiceModel.Channels; 
using System.Diagnostics;
using System.IO; 
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Transactions; 
using System.Xml;
using Microsoft.Transactions.Wsat.Protocol; 
 
namespace Microsoft.Transactions.Wsat.Messaging
{ 
//---------------------------------------------------------------------------------------------------
    //
    // Messages
    // 
//---------------------------------------------------------------------------------------------------
    abstract class CoordinationMessage : Message 
    { 
        MessageHeaders headers;
        MessageProperties properties; 

        protected CoordinationMessage(string action, MessageVersion version)
        {
            this.headers = new MessageHeaders(version); 
            this.headers.Action = action;
        } 
 
        public override MessageHeaders Headers
        { 
            get { return this.headers; }
        }

        public override MessageProperties Properties 
        {
            get 
            { 
                if (this.properties == null)
                { 
                    lock (this)
                    {
                        if (this.properties == null)
                        { 
                            this.properties = new MessageProperties();
                        } 
                    } 
                }
 
                return this.properties;
            }
        }
 
        public override MessageVersion Version
        { 
            get { return this.headers.MessageVersion; } 
        }
    } 

//----------------------------------------------------------------------------------------------------
    //
    // Activation 
    //
//--------------------------------------------------------------------------------------------------- 
    class CreateCoordinationContextMessage : CoordinationMessage 
    {
        CreateCoordinationContext create; 

        public CreateCoordinationContextMessage(MessageVersion version, ref CreateCoordinationContext create)
            :
            base(CoordinationStrings.Version(create.ProtocolVersion).CreateCoordinationContextAction, version) 
        {
            this.create = create; 
        } 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer) 
        {
            if (writer == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException ("writer"));
 
            this.create.WriteTo(writer);
        } 
    } 

    class CreateCoordinationContextResponseMessage : CoordinationMessage 
    {
        CreateCoordinationContextResponse response;

        public CreateCoordinationContextResponseMessage(MessageVersion version, ref CreateCoordinationContextResponse response) 
            :
            base(CoordinationStrings.Version(response.ProtocolVersion).CreateCoordinationContextResponseAction, version) 
        { 
            this.response = response;
        } 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        {
            if (writer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException ("writer"));
 
            this.response.WriteTo(writer); 
        }
    } 

//----------------------------------------------------------------------------------------------------
    //
    // Registration 
    //
//---------------------------------------------------------------------------------------------------- 
    class RegisterMessage : CoordinationMessage 
    {
        Register register; 

        public RegisterMessage(MessageVersion version, ref Register registerBody)
            :
            base(CoordinationStrings.Version(registerBody.ProtocolVersion).RegisterAction, version) 
        {
            this.register = registerBody; 
        } 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer) 
        {
            if (writer == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException ("writer"));
 
            register.WriteTo(writer);
        } 
    } 

    class RegisterResponseMessage : CoordinationMessage 
    {
        RegisterResponse response;

        public RegisterResponseMessage(MessageVersion version, ref RegisterResponse response) 
            :
            base(CoordinationStrings.Version(response.ProtocolVersion).RegisterResponseAction, version) 
        { 
            this.response = response;
        } 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        {
            if (writer == null) 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException ("writer"));
 
            response.WriteTo(writer); 
        }
    } 



//--------------------------------------------------------------------------------------------------- 
    //
    // WS-AT messages 
    // 
//----------------------------------------------------------------------------------------------------
    abstract class NotificationMessage : CoordinationMessage 
    {
        protected AtomicTransactionStrings atomicTransactionStrings;
        protected AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings;
 
        protected NotificationMessage(string action, MessageVersion version, ProtocolVersion protocolVersion) :
            base(action, version) 
        { 
            this.atomicTransactionStrings = AtomicTransactionStrings.Version(protocolVersion);
            this.atomicTransactionXmlDictionaryStrings = AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 
        }

        protected void WriteTo(XmlDictionaryWriter writer, string prefix, XmlDictionaryString localName, XmlDictionaryString ns)
        { 
            writer.WriteStartElement(prefix, localName, ns);
            writer.WriteEndElement(); 
        } 

        protected static void ReadFrom(Message message, XmlDictionaryString localName, XmlDictionaryString ns) 
        {
            if (message.IsEmpty)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody))); 
            }
 
            using (XmlDictionaryReader reader = message.GetReaderAtBodyContents()) 
            {
                ReadFrom(reader, localName, ns); 
                try
                {
                    message.ReadFromBodyContentsToEnd(reader);
                } 
                catch (XmlException e)
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e)); 
                }
            } 
        }

        protected static void ReadFrom(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
        { 
            try
            { 
                if (!reader.IsStartElement (localName, ns)) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody))); 
                }

                bool empty = reader.IsEmptyElement;
                reader.ReadStartElement(); 

                if (!empty) 
                { 
                    // Play nicely with any elements that might be inside the notification element
                    while (reader.IsStartElement()) 
                    {
                        reader.Skip();
                    }
                    reader.ReadEndElement(); 
                }
            } 
            catch (XmlException e) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException (SR.GetString (SR.InvalidMessageBody), e)); 
            }
        }

        public static Message CreateRecoverMessage(MessageVersion version, ProtocolVersion protocolVersion) 
        {
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(NotificationMessage), "CreateRecoverMessage"); 
 
            switch (protocolVersion)
            { 
                case ProtocolVersion.Version10 :
                    return new ReplayMessage(version, protocolVersion);

                case ProtocolVersion.Version11 : 
                    return new PreparedMessage(version, protocolVersion);
 
                default : 
                    return null; // inaccessible path because we have asserted the protocol version
            } 
        }
    }

//--------------------------------------------------------------------------------------------------- 
    class PrepareMessage : NotificationMessage
    { 
 
        public PrepareMessage(MessageVersion version, ProtocolVersion protocolVersion)
            : 
            base(AtomicTransactionStrings.Version(protocolVersion).PrepareAction, version, protocolVersion) {}

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        { 
            WriteTo(writer,
                    this.atomicTransactionStrings.Prefix, 
                    this.atomicTransactionXmlDictionaryStrings.Prepare, 
                    this.atomicTransactionXmlDictionaryStrings.Namespace);
        } 

        public static void ReadFrom(Message message, ProtocolVersion protocolVersion)
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);
 
            ReadFrom(message, 
                    atomicTransactionXmlDictionaryStrings.Prepare,
                    atomicTransactionXmlDictionaryStrings.Namespace); 
        }

        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 
 
            ReadFrom(reader,
                    atomicTransactionXmlDictionaryStrings.Prepare, 
                    atomicTransactionXmlDictionaryStrings.Namespace);
        }
    }
 
//---------------------------------------------------------------------------------------------------
    class PreparedMessage : NotificationMessage 
    { 

        public PreparedMessage(MessageVersion version, ProtocolVersion protocolVersion) 
            :
            base(AtomicTransactionStrings.Version(protocolVersion).PreparedAction, version, protocolVersion) {}

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer) 
        {
            WriteTo(writer, 
                    this.atomicTransactionStrings.Prefix, 
                    this.atomicTransactionXmlDictionaryStrings.Prepared,
                    this.atomicTransactionXmlDictionaryStrings.Namespace); 
        }

        public static void ReadFrom(Message message, ProtocolVersion protocolVersion)
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 
 
            ReadFrom(message,
                     atomicTransactionXmlDictionaryStrings.Prepared, 
                     atomicTransactionXmlDictionaryStrings.Namespace);
        }

        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 

            ReadFrom(reader, 
                     atomicTransactionXmlDictionaryStrings.Prepared,
                     atomicTransactionXmlDictionaryStrings.Namespace);
        }
    } 

//--------------------------------------------------------------------------------------------------- 
    class ReadOnlyMessage : NotificationMessage 
    {
 
        public ReadOnlyMessage(MessageVersion version, ProtocolVersion protocolVersion)
            :
            base(AtomicTransactionStrings.Version(protocolVersion).ReadOnlyAction, version, protocolVersion) {}
 
        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        { 
            WriteTo(writer, 
                    this.atomicTransactionStrings.Prefix,
                    this.atomicTransactionXmlDictionaryStrings.ReadOnly, 
                    this.atomicTransactionXmlDictionaryStrings.Namespace);
        }

        public static void ReadFrom(Message message, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 

            ReadFrom(message, 
                     atomicTransactionXmlDictionaryStrings.ReadOnly,
                     atomicTransactionXmlDictionaryStrings.Namespace);
        }
 
        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);
 
            ReadFrom(reader,
                     atomicTransactionXmlDictionaryStrings.ReadOnly,
                     atomicTransactionXmlDictionaryStrings.Namespace);
        } 
    }
 
//---------------------------------------------------------------------------------------------------- 
    class CommitMessage : NotificationMessage
    { 

        public CommitMessage(MessageVersion version, ProtocolVersion protocolVersion)
            :
            base(AtomicTransactionStrings.Version(protocolVersion).CommitAction, version, protocolVersion) {} 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer) 
        { 

            WriteTo(writer, 
                    this.atomicTransactionStrings.Prefix,
                    this.atomicTransactionXmlDictionaryStrings.Commit,
                    this.atomicTransactionXmlDictionaryStrings.Namespace);
        } 

        public static void ReadFrom(Message message, ProtocolVersion protocolVersion) 
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 

            ReadFrom(message,
                     atomicTransactionXmlDictionaryStrings.Commit,
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        }
 
        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);

            ReadFrom(reader,
                     atomicTransactionXmlDictionaryStrings.Commit, 
                     atomicTransactionXmlDictionaryStrings.Namespace);
        } 
    } 

//--------------------------------------------------------------------------------------------------- 
    class RollbackMessage : NotificationMessage
    {

        public RollbackMessage(MessageVersion version, ProtocolVersion protocolVersion) 
            :
            base(AtomicTransactionStrings.Version(protocolVersion).RollbackAction, version, protocolVersion) {} 
 
        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        { 
            WriteTo(writer,
                    this.atomicTransactionStrings.Prefix,
                    this.atomicTransactionXmlDictionaryStrings.Rollback,
                    this.atomicTransactionXmlDictionaryStrings.Namespace); 
        }
 
        public static void ReadFrom(Message message, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);

            ReadFrom(message,
                     atomicTransactionXmlDictionaryStrings.Rollback, 
                     atomicTransactionXmlDictionaryStrings.Namespace);
        } 
 
        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);

            ReadFrom(reader, 
                     atomicTransactionXmlDictionaryStrings.Rollback,
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        } 
    }
 
//----------------------------------------------------------------------------------------------------
    class CommittedMessage : NotificationMessage
    {
 
        public CommittedMessage(MessageVersion version, ProtocolVersion protocolVersion)
            : 
            base(AtomicTransactionStrings.Version(protocolVersion).CommittedAction, version, protocolVersion) {} 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer) 
        {
            WriteTo(writer,
                    this.atomicTransactionStrings.Prefix,
                    this.atomicTransactionXmlDictionaryStrings.Committed, 
                    this.atomicTransactionXmlDictionaryStrings.Namespace);
        } 
 
        public static void ReadFrom(Message message, ProtocolVersion protocolVersion)
        { 
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);

            ReadFrom(message, 
                     atomicTransactionXmlDictionaryStrings.Committed,
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        } 

        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);
 
            ReadFrom(reader,
                     atomicTransactionXmlDictionaryStrings.Committed, 
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        }
    } 

//----------------------------------------------------------------------------------------------------
    class AbortedMessage : NotificationMessage
    { 

        public AbortedMessage(MessageVersion version, ProtocolVersion protocolVersion) 
            : 
            base(AtomicTransactionStrings.Version(protocolVersion).AbortedAction, version, protocolVersion) {}
 
        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        {
            WriteTo(writer,
                    this.atomicTransactionStrings.Prefix, 
                    this.atomicTransactionXmlDictionaryStrings.Aborted,
                    this.atomicTransactionXmlDictionaryStrings.Namespace); 
        } 

        public static void ReadFrom(Message message, ProtocolVersion protocolVersion) 
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);
 
            ReadFrom(message,
                     atomicTransactionXmlDictionaryStrings.Aborted, 
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        }
 
        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 

            ReadFrom(reader, 
                     atomicTransactionXmlDictionaryStrings.Aborted, 
                     atomicTransactionXmlDictionaryStrings.Namespace);
        } 
    }

//---------------------------------------------------------------------------------------------------
    class ReplayMessage : NotificationMessage 
    {
 
        public ReplayMessage(MessageVersion version, ProtocolVersion protocolVersion) 
            :
            base(AtomicTransactionStrings.Version(protocolVersion).ReplayAction, version, protocolVersion) {} 

        protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
        {
            WriteTo(writer, 
                    this.atomicTransactionStrings.Prefix,
                    this.atomicTransactionXmlDictionaryStrings.Replay, 
                    this.atomicTransactionXmlDictionaryStrings.Namespace); 
        }
 
        public static void ReadFrom(Message message, ProtocolVersion protocolVersion)
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings =
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 

            ReadFrom(message, 
                     atomicTransactionXmlDictionaryStrings.Replay, 
                     atomicTransactionXmlDictionaryStrings.Namespace);
        } 

        public static void ReadFrom(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
        {
            AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings = 
                                    AtomicTransactionXmlDictionaryStrings.Version(protocolVersion);
 
            ReadFrom(reader, 
                     atomicTransactionXmlDictionaryStrings.Replay,
                     atomicTransactionXmlDictionaryStrings.Namespace); 
        }
    }
}

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