WebScriptMetadataMessageEncoderFactory.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Channels / WebScriptMetadataMessageEncoderFactory.cs / 1305376 / WebScriptMetadataMessageEncoderFactory.cs

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

namespace System.ServiceModel.Channels 
{
    using System.Globalization; 
    using System.IO; 
    using System.Net.Mime;
    using System.ServiceModel.Diagnostics; 
    using System.Text;
    using System.Xml;

    class WebScriptMetadataMessageEncoderFactory : MessageEncoderFactory 
    {
        const string applicationJavaScriptMediaType = "application/x-javascript"; 
        WebScriptMetadataMessageEncoder messageEncoder; 

        public WebScriptMetadataMessageEncoderFactory(XmlDictionaryReaderQuotas quotas) 
        {
            messageEncoder = new WebScriptMetadataMessageEncoder(quotas);
        }
 
        public override MessageEncoder Encoder
        { 
            get { return messageEncoder; } 
        }
 
        public override MessageVersion MessageVersion
        {
            get { return messageEncoder.MessageVersion; }
        } 

        class WebScriptMetadataMessageEncoder : MessageEncoder 
        { 
            static UTF8Encoding UTF8EncodingWithoutByteOrderMark = new UTF8Encoding(false);
            string contentType; 
            MessageEncoder innerReadMessageEncoder;
            string mediaType;
            XmlDictionaryReaderQuotas readerQuotas;
 
            public WebScriptMetadataMessageEncoder(XmlDictionaryReaderQuotas quotas)
            { 
                this.readerQuotas = new XmlDictionaryReaderQuotas(); 
                quotas.CopyTo(this.readerQuotas);
                this.mediaType = this.contentType = applicationJavaScriptMediaType; 
                this.innerReadMessageEncoder = new TextMessageEncodingBindingElement(MessageVersion.None, Encoding.UTF8).CreateMessageEncoderFactory().Encoder;
            }

            public override string ContentType 
            {
                get { return contentType; } 
            } 

            public override string MediaType 
            {
                get { return mediaType; }
            }
 
            public override MessageVersion MessageVersion
            { 
                get { return MessageVersion.None; } 
            }
 
            public override bool IsContentTypeSupported(string contentType)
            {
                return innerReadMessageEncoder.IsContentTypeSupported(contentType);
            } 

            public override Message ReadMessage(ArraySegment buffer, BufferManager bufferManager, string contentType) 
            { 
                return innerReadMessageEncoder.ReadMessage(buffer, bufferManager, contentType);
            } 

            public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
            {
                return innerReadMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType); 
            }
 
            public override ArraySegment WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) 
            {
                if (message == null) 
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
                }
                if (bufferManager == null) 
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("bufferManager"), message); 
                } 
                if (maxMessageSize < 0)
                { 
                    throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize,
                        SR2.GetString(SR2.ValueMustBeNonNegative)), message);
                }
                if (messageOffset < 0 || messageOffset > maxMessageSize) 
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageOffset", messageOffset, 
                        SR2.GetString(SR2.JsonValueMustBeInRange, 0, maxMessageSize)), message); 
                }
 
                ThrowIfMismatchedMessageVersion(message);
                message.Properties.Encoder = this;
                BufferedMessageWriter messageWriter = new WebScriptMetadataBufferedMessageWriter(this);
                ArraySegment messageData = messageWriter.WriteMessage(message, bufferManager, messageOffset, maxMessageSize); 
                if (MessageLogger.LogMessagesAtTransportLevel)
                { 
                    MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); 
                }
                return messageData; 
            }

            public override void WriteMessage(Message message, Stream stream)
            { 
                if (message == null)
                { 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); 
                }
                if (stream == null) 
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message);
                }
                ThrowIfMismatchedMessageVersion(message); 
                message.Properties.Encoder = this;
                XmlDictionaryWriter xmlWriter = CreateWriter(stream); 
                xmlWriter.WriteStartDocument(); 
                message.WriteMessage(xmlWriter);
                xmlWriter.WriteEndDocument(); 
                xmlWriter.Flush();
                xmlWriter.Close();
                if (MessageLogger.LogMessagesAtTransportLevel)
                { 
                    MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend);
                } 
            } 

            XmlDictionaryWriter CreateWriter(Stream stream) 
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Encoding = UTF8EncodingWithoutByteOrderMark; 
                XmlWriter writer = XmlWriter.Create(stream, settings);
                return XmlDictionaryWriter.CreateDictionaryWriter(writer); 
            } 

            class WebScriptMetadataBufferedMessageWriter : BufferedMessageWriter 
            {
                WebScriptMetadataMessageEncoder messageEncoder;

                public WebScriptMetadataBufferedMessageWriter(WebScriptMetadataMessageEncoder messageEncoder) 
                {
                    this.messageEncoder = messageEncoder; 
                } 

                protected override void OnWriteEndMessage(XmlDictionaryWriter writer) 
                {
                }

                protected override void OnWriteStartMessage(XmlDictionaryWriter writer) 
                {
                } 
 
                protected override void ReturnXmlWriter(XmlDictionaryWriter writer)
                { 
                    writer.Close();
                }

                protected override XmlDictionaryWriter TakeXmlWriter(Stream stream) 
                {
                    return messageEncoder.CreateWriter(stream); 
                } 
            }
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

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