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

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

// Defines wire representations of WS-Coor, WS-AT and .NET-AT message bodies 

using System; 
using System.ServiceModel.Channels; 
using System.Diagnostics;
using System.IO; 
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Text;
using System.Transactions; 
using System.Xml;
using Microsoft.Transactions.Wsat.Protocol; 
 
namespace Microsoft.Transactions.Wsat.Messaging
{ 
//=====================================================================================
//                       Activation Structures
//=====================================================================================
 
    struct CreateCoordinationContext
    { 
 
        CoordinationXmlDictionaryStrings coordinationXmlDictionaryStrings;
        AtomicTransactionXmlDictionaryStrings atomicTransactionXmlDictionaryStrings; 
        AtomicTransactionStrings atomicTransactionStrings;
        CoordinationStrings coordinationStrings;

        ProtocolVersion protocolVersion; 
        public ProtocolVersion ProtocolVersion
        { 
            get { return this.protocolVersion; } 
        }
 
        bool expiresPresent;
        public bool ExpiresPresent
        {
            get { return this.expiresPresent; } 
        }
 
        uint expiration; 
        public uint Expires
        { 
            get { return expiration; }
            set
            {
                this.expiration = value; 
                this.expiresPresent = true;
            } 
        } 

        public CoordinationContext CurrentContext; 
        public RequestSecurityTokenResponse IssuedToken;

        //
        // Extensions 
        //
 
        public IsolationLevel IsolationLevel; 

        public CreateCoordinationContext(ProtocolVersion protocolVersion) 
        {
            this.expiration = 0;
            this.expiresPresent = false;
            this.CurrentContext = null; 
            this.IssuedToken = null;
            this.IsolationLevel = IsolationLevel.Serializable; 
 
            // protocol version related members
            this.protocolVersion = protocolVersion; 
            this.coordinationXmlDictionaryStrings =
                CoordinationXmlDictionaryStrings.Version(protocolVersion);
            this.atomicTransactionXmlDictionaryStrings =
                AtomicTransactionXmlDictionaryStrings.Version(protocolVersion); 
            this.atomicTransactionStrings =
                AtomicTransactionStrings.Version(protocolVersion); 
            this.coordinationStrings = 
                CoordinationStrings.Version(protocolVersion);
        } 

        public CreateCoordinationContext(Message message, ProtocolVersion protocolVersion) :
            this (protocolVersion)
 
        {
            if (message.IsEmpty) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody)));
            } 

            XmlDictionaryReader reader = message.GetReaderAtBodyContents();
            using (reader)
            { 
                this.ReadFrom(reader);
                try 
                { 
                    message.ReadFromBodyContentsToEnd(reader);
                } 
                catch (XmlException e)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
                } 
            }
 
            // Look for a transaction token header if a coordination context is present 
            if (this.CurrentContext != null)
            { 
                try
                {
                    this.IssuedToken = CoordinationServiceSecurity.GetIssuedToken(message,
                                                                                  this.CurrentContext.Identifier, 
                                                                                  protocolVersion);
                } 
                catch (XmlException e) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                        new InvalidMessageException(e.Message, e));
                }
            }
        } 

        public CreateCoordinationContext(XmlDictionaryReader reader, ProtocolVersion protocolVersion) : 
            this (protocolVersion) 
        {
            this.ReadFrom(reader); 
        }

        void ReadFrom(XmlDictionaryReader reader)
        { 
            string element;
 
            try 
            {
                reader.ReadFullStartElement(this.coordinationXmlDictionaryStrings.CreateCoordinationContext, 
                                            this.coordinationXmlDictionaryStrings.Namespace);

                if (reader.IsStartElement(this.coordinationXmlDictionaryStrings.Expires,
                                          this.coordinationXmlDictionaryStrings.Namespace)) 
                {
                    int expires = reader.ReadElementContentAsInt(); 
                    if (expires < 0) 
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody))); 
                    }
                    this.expiration = (uint)expires;
                    this.expiresPresent = true;
                } 

                if (reader.IsStartElement(this.coordinationXmlDictionaryStrings.CurrentContext, 
                                          this.coordinationXmlDictionaryStrings.Namespace)) 
                {
                    this.CurrentContext = CoordinationContext.ReadFrom(reader, 
                                                                       this.coordinationXmlDictionaryStrings.CurrentContext,
                                                                       this.coordinationXmlDictionaryStrings.Namespace,
                                                                       this.protocolVersion);
                } 

                reader.MoveToStartElement(this.coordinationXmlDictionaryStrings.CoordinationType, 
                                          this.coordinationXmlDictionaryStrings.Namespace); 

                element = reader.ReadElementContentAsString().Trim(); 
                if (element != this.atomicTransactionStrings.Namespace)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody)));
                } 

                // 
                // Extensions 
                //
 
                if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.IsolationLevel,
                                          XD.DotNetAtomicTransactionExternalDictionary.Namespace))
                {
                    this.IsolationLevel = (IsolationLevel)reader.ReadElementContentAsInt(); 
                    if (this.IsolationLevel < IsolationLevel.Serializable ||
                        this.IsolationLevel > IsolationLevel.Unspecified || 
                        this.IsolationLevel == IsolationLevel.Snapshot) 
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody))); 
                    }
                }

                // Play nicely with any unexpected elements that might be here 
                while (reader.IsStartElement())
                { 
                    reader.Skip(); 
                }
                reader.ReadEndElement(); 
            }
            catch (XmlException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e)); 
            }
            catch (InvalidCoordinationContextException e) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
            } 
        }

        public void WriteTo(XmlDictionaryWriter writer)
        { 
            writer.WriteStartElement(this.coordinationStrings.Prefix,
                                     this.coordinationXmlDictionaryStrings.CreateCoordinationContext, 
                                     this.coordinationXmlDictionaryStrings.Namespace); 

            if (this.expiresPresent) 
            {
                writer.WriteStartElement(this.coordinationXmlDictionaryStrings.Expires,
                                         this.coordinationXmlDictionaryStrings.Namespace);
                writer.WriteValue(this.Expires); 
                writer.WriteEndElement();
            } 
 
            if (this.CurrentContext != null)
            { 
                this.CurrentContext.WriteTo(writer,
                                            this.coordinationXmlDictionaryStrings.CurrentContext,
                                            this.coordinationXmlDictionaryStrings.Namespace);
            } 

            writer.WriteStartElement(this.coordinationXmlDictionaryStrings.CoordinationType, 
                                     this.coordinationXmlDictionaryStrings.Namespace); 
            writer.WriteString(this.atomicTransactionXmlDictionaryStrings.Namespace);
            writer.WriteEndElement(); 

            //
            // Extensions
            // 

            if (this.IsolationLevel != IsolationLevel.Serializable) 
            { 
                writer.WriteStartElement(DotNetAtomicTransactionExternalStrings.Prefix,
                                         XD.DotNetAtomicTransactionExternalDictionary.IsolationLevel, 
                                         XD.DotNetAtomicTransactionExternalDictionary.Namespace);
                writer.WriteValue((int)this.IsolationLevel);
                writer.WriteEndElement();
            } 

            writer.WriteEndElement(); 
        } 
    }
 
    //======================================================================================

    struct CreateCoordinationContextResponse
    { 
        public CoordinationContext CoordinationContext;
        public RequestSecurityTokenResponse IssuedToken; 
        CoordinationStrings coordinationStrings; 
        CoordinationXmlDictionaryStrings coordinationXmlDictionaryStrings;
 
        ProtocolVersion protocolVersion;
        public ProtocolVersion ProtocolVersion
        {
            get { return this.protocolVersion; } 
        }
 
        // 
        // Extensibility
        // 

        public CreateCoordinationContextResponse(ProtocolVersion protocolVersion)
        {
            this.CoordinationContext = null; 
            this.IssuedToken = null;
            this.protocolVersion = protocolVersion; 
            this.coordinationStrings = CoordinationStrings.Version(protocolVersion); 
            this.coordinationXmlDictionaryStrings = CoordinationXmlDictionaryStrings.Version(protocolVersion);
        } 


        public CreateCoordinationContextResponse(Message message, ProtocolVersion protocolVersion) :
            this(protocolVersion) 
        {
 
            if (message.IsEmpty) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody))); 
            }

            XmlDictionaryReader reader = message.GetReaderAtBodyContents();
            using (reader) 
            {
                this.ReadFrom(reader); 
                try 
                {
                    message.ReadFromBodyContentsToEnd(reader); 
                }
                catch (XmlException e)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e)); 
                }
            } 
 
            try
            { 
                this.IssuedToken = CoordinationServiceSecurity.GetIssuedToken(message,
                                                                              this.CoordinationContext.Identifier,
                                                                              protocolVersion);
            } 
            catch (XmlException e)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( 
                    new InvalidMessageException(e.Message, e));
            } 
        }

        public CreateCoordinationContextResponse(XmlDictionaryReader reader, ProtocolVersion protocolVersion) :
            this(protocolVersion) 
        {
            this.ReadFrom(reader); 
        } 

        void ReadFrom(XmlDictionaryReader reader) 
        {
            try
            {
                reader.ReadFullStartElement(this.coordinationXmlDictionaryStrings.CreateCoordinationContextResponse, 
                                            this.coordinationXmlDictionaryStrings.Namespace);
 
                this.CoordinationContext = CoordinationContext.ReadFrom(reader, 
                                                                        this.coordinationXmlDictionaryStrings.CoordinationContext,
                                                                        this.coordinationXmlDictionaryStrings.Namespace, 
                                                                        this.protocolVersion);

                // Play nicely with any unexpected elements that might be here
                while (reader.IsStartElement()) 
                {
                    reader.Skip(); 
                } 
                reader.ReadEndElement();
            } 
            catch (XmlException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
            } 
            catch (InvalidCoordinationContextException e)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e)); 
            }
        } 

        public void WriteTo(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement(this.coordinationStrings.Prefix, 
                                     this.coordinationXmlDictionaryStrings.CreateCoordinationContextResponse,
                                     this.coordinationXmlDictionaryStrings.Namespace); 
 
            this.CoordinationContext.WriteTo(writer,
                                             this.coordinationXmlDictionaryStrings.CoordinationContext, 
                                             this.coordinationXmlDictionaryStrings.Namespace);
            writer.WriteEndElement();
        }
    } 

 
 
//=====================================================================================
//                              Registration structures 
//======================================================================================

    struct Register
    { 
        public ControlProtocol Protocol;
        public EndpointAddress ParticipantProtocolService; 
        CoordinationStrings coordinationStrings; 
        CoordinationXmlDictionaryStrings coordinationXmlDictionaryStrings;
 
        //
        // Extensions
        //
 
        public Guid Loopback;
 
        // Local-only, not serialized 
        public RequestSecurityTokenResponse SupportingToken;
 
        // protocol version
        ProtocolVersion protocolVersion;
        public ProtocolVersion ProtocolVersion
        { 
            get { return this.protocolVersion; }
        } 
 
        public Register(ProtocolVersion protocolVersion)
        { 
            this.Protocol = ControlProtocol.None;
            this.ParticipantProtocolService = null;
            this.Loopback = Guid.Empty;
            this.SupportingToken = null; 

            this.protocolVersion = protocolVersion; 
            this.coordinationStrings = CoordinationStrings.Version(protocolVersion); 
            this.coordinationXmlDictionaryStrings = CoordinationXmlDictionaryStrings.Version(protocolVersion);
        } 

        public Register(Message message, ProtocolVersion protocolVersion) :
            this(protocolVersion)
        { 

            if (message.IsEmpty) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody)));
            } 

            XmlDictionaryReader reader = message.GetReaderAtBodyContents();
            using (reader)
            { 
                this.ReadFrom(reader);
                try 
                { 
                    message.ReadFromBodyContentsToEnd(reader);
                } 
                catch (XmlException e)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
                } 
            }
        } 
 
        public Register(XmlDictionaryReader reader, ProtocolVersion protocolVersion) :
            this(protocolVersion) 
        {
            this.ReadFrom(reader);
        }
 
        void ReadFrom(XmlDictionaryReader reader)
        { 
            try 
            {
                reader.ReadFullStartElement(this.coordinationXmlDictionaryStrings.Register, 
                                            this.coordinationXmlDictionaryStrings.Namespace);

                reader.MoveToStartElement(this.coordinationXmlDictionaryStrings.Protocol,
                                          this.coordinationXmlDictionaryStrings.Namespace); 

                string element = reader.ReadElementContentAsString(); 
 
                this.Protocol = WSAtomicTransactionStrings.WellKnownNameToProtocol(element.Trim(), this.protocolVersion);
                if (this.Protocol == ControlProtocol.None) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody)));
                }
 
                this.ParticipantProtocolService = EndpointAddress.ReadFrom(MessagingVersionHelper.AddressingVersion(this.protocolVersion),
                                                                           reader, 
                                                                           this.coordinationXmlDictionaryStrings.ParticipantProtocolService, 
                                                                           this.coordinationXmlDictionaryStrings.Namespace);
 
                //
                // Extensions
                //
 
                if (reader.IsStartElement(XD.DotNetAtomicTransactionExternalDictionary.Loopback,
                                          XD.DotNetAtomicTransactionExternalDictionary.Namespace)) 
                { 
                    this.Loopback = reader.ReadElementContentAsGuid();
                } 

                // Play nicely with any unexpected elements that might be here
                while (reader.IsStartElement())
                { 
                    reader.Skip();
                } 
                reader.ReadEndElement(); 
            }
            catch (XmlException e) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
            }
        } 

        public void WriteTo(XmlDictionaryWriter writer) 
        { 
            writer.WriteStartElement(this.coordinationStrings.Prefix,
                                     this.coordinationXmlDictionaryStrings.Register, 
                                     this.coordinationXmlDictionaryStrings.Namespace);

            XmlDictionaryString uri = WSAtomicTransactionStrings.ProtocolToWellKnownName(this.Protocol, this.protocolVersion);
            writer.WriteStartElement(this.coordinationXmlDictionaryStrings.Protocol, 
                                     this.coordinationXmlDictionaryStrings.Namespace);
            writer.WriteString(uri); 
            writer.WriteEndElement(); 

            this.ParticipantProtocolService.WriteTo(MessagingVersionHelper.AddressingVersion(this.protocolVersion), 
                                                    writer,
                                                    this.coordinationXmlDictionaryStrings.ParticipantProtocolService,
                                                    this.coordinationXmlDictionaryStrings.Namespace);
 
            if (this.Loopback != Guid.Empty)
            { 
                writer.WriteStartElement(DotNetAtomicTransactionExternalStrings.Prefix, 
                                         XD.DotNetAtomicTransactionExternalDictionary.Loopback,
                                         XD.DotNetAtomicTransactionExternalDictionary.Namespace); 
                writer.WriteValue(this.Loopback);
                writer.WriteEndElement();
            }
 
            writer.WriteEndElement();
        } 
    } 

    //====================================================================================== 
    struct RegisterResponse
    {
        public EndpointAddress CoordinatorProtocolService;
        CoordinationStrings coordinationStrings; 
        CoordinationXmlDictionaryStrings coordinationXmlDictionaryStrings;
 
        // protocol version 
        ProtocolVersion protocolVersion;
        public ProtocolVersion ProtocolVersion 
        {
            get { return this.protocolVersion; }
        }
 
        public RegisterResponse(ProtocolVersion protocolVersion)
        { 
            this.CoordinatorProtocolService = null; 
            this.protocolVersion = protocolVersion;
            this.coordinationStrings = CoordinationStrings.Version(protocolVersion); 
            this.coordinationXmlDictionaryStrings = CoordinationXmlDictionaryStrings.Version(protocolVersion);

        }
 
        public RegisterResponse(Message message, ProtocolVersion protocolVersion) :
            this(protocolVersion) 
        { 
            if (message.IsEmpty)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody)));
            }

            XmlDictionaryReader reader = message.GetReaderAtBodyContents(); 
            using (reader)
            { 
                this.ReadFrom(reader); 
                try
                { 
                    message.ReadFromBodyContentsToEnd(reader);
                }
                catch (XmlException e)
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
                } 
            } 
        }
 
        public RegisterResponse(XmlDictionaryReader reader, ProtocolVersion protocolVersion) :
            this(protocolVersion)
        {
            this.ReadFrom(reader); 
        }
 
        void ReadFrom(XmlDictionaryReader reader) 
        {
            try 
            {
                reader.ReadFullStartElement(this.coordinationXmlDictionaryStrings.RegisterResponse,
                                            this.coordinationXmlDictionaryStrings.Namespace);
 
                this.CoordinatorProtocolService = EndpointAddress.ReadFrom(MessagingVersionHelper.AddressingVersion(this.protocolVersion),
                                                                           reader, 
                                                                           this.coordinationXmlDictionaryStrings.CoordinatorProtocolService, 
                                                                           this.coordinationXmlDictionaryStrings.Namespace);
 
                // Play nicely with any unexpected elements that might be here
                while (reader.IsStartElement())
                {
                    reader.Skip(); 
                }
                reader.ReadEndElement(); 
            } 
            catch (XmlException e)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidMessageException(SR.GetString(SR.InvalidMessageBody), e));
            }
        }
 
        public void WriteTo(XmlDictionaryWriter writer)
        { 
            writer.WriteStartElement(this.coordinationStrings.Prefix, 
                                     this.coordinationXmlDictionaryStrings.RegisterResponse,
                                     this.coordinationXmlDictionaryStrings.Namespace); 

            this.CoordinatorProtocolService.WriteTo(MessagingVersionHelper.AddressingVersion(this.protocolVersion),
                                                    writer,
                                                    this.coordinationXmlDictionaryStrings.CoordinatorProtocolService, 
                                                    this.coordinationXmlDictionaryStrings.Namespace);
            writer.WriteEndElement(); 
        } 
    }
 

//===============================================================================
//                                  Helper class
//================================================================================ 
    static class MessagingVersionHelper
    { 
        public static AddressingVersion AddressingVersion(ProtocolVersion protocolVersion) 
        {
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(MessagingVersionHelper), "AddressingVersion"); //assert valid protocol version 

            switch (protocolVersion)
            {
                case ProtocolVersion.Version10: 
                    return System.ServiceModel.Channels.AddressingVersion.WSAddressingAugust2004;
 
                case ProtocolVersion.Version11: 
                    return System.ServiceModel.Channels.AddressingVersion.WSAddressing10;
 
                default:
                    return null; // inaccessible path because we have asserted the protocol version
            }
        } 

        public static MessageVersion MessageVersion(ProtocolVersion protocolVersion) 
        { 
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(MessagingVersionHelper), "MessageVersion"); //assert valid protocol version
 
            switch (protocolVersion)
            {
                case ProtocolVersion.Version10:
                    return System.ServiceModel.Channels.MessageVersion.Soap11WSAddressingAugust2004; 

                case ProtocolVersion.Version11: 
                    return System.ServiceModel.Channels.MessageVersion.Soap11WSAddressing10; 

                default: 
                    return null; // inaccessible path because we have asserted the protocol version
            }
        }
 
        public static MessageSecurityVersion SecurityVersion(ProtocolVersion protocolVersion)
        { 
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(MessagingVersionHelper), "SecurityVersion"); //assert valid protocol version 

            switch (protocolVersion) 
            {
                case ProtocolVersion.Version10:
                    return MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;
 
                case ProtocolVersion.Version11:
                    return MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12; 
 
                default:
                    return null; // inaccessible path because we have asserted the protocol version 
            }
        }

        public static void SetReplyAddress(Message message, EndpointAddress replyTo, ProtocolVersion protocolVersion) 
        {
            ProtocolVersionHelper.AssertProtocolVersion(protocolVersion, typeof(MessagingVersionHelper), "SetReplyAddress"); //assert valid protocol version 
 
            switch (protocolVersion)
            { 
                case ProtocolVersion.Version10:
                    if(message.Headers.ReplyTo == null)
                        message.Headers.ReplyTo = replyTo;
 
                    // CSD 4085 -- All messages with ReplyTo must also have a MessageId
                    if(message.Headers.MessageId == null) 
                        message.Headers.MessageId = new UniqueId(); 
                    return;
 
                case ProtocolVersion.Version11:
                    if(message.Headers.From == null)
                        message.Headers.From = replyTo;
                    return; 

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

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