public override ArraySegment <byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
            {
                BinaryMessageEncoderFactory.BinaryBufferedMessageWriter sessionMessageWriter;
                if (message == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
                }
                if (bufferManager == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bufferManager");
                }
                if (maxMessageSize < 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, System.ServiceModel.SR.GetString("ValueMustBeNonNegative")));
                }
                message.Properties.Encoder = this;
                if (this.isSession)
                {
                    if (this.writerSession == null)
                    {
                        this.writerSession        = new BinaryMessageEncoderFactory.XmlBinaryWriterSessionWithQuota(this.maxSessionSize);
                        this.sessionMessageWriter = new BinaryMessageEncoderFactory.BinaryBufferedMessageWriter(this.factory.binaryVersion.Dictionary, this.writerSession);
                    }
                    messageOffset += 5;
                }
                if (messageOffset < 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageOffset", messageOffset, System.ServiceModel.SR.GetString("ValueMustBeNonNegative")));
                }
                if (messageOffset > maxMessageSize)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(System.ServiceModel.SR.GetString("MaxSentMessageSizeExceeded", new object[] { maxMessageSize })));
                }
                base.ThrowIfMismatchedMessageVersion(message);
                if (this.isSession)
                {
                    sessionMessageWriter = this.sessionMessageWriter;
                }
                else
                {
                    sessionMessageWriter = this.factory.TakeBufferedWriter();
                }
                ArraySegment <byte> messageData = sessionMessageWriter.WriteMessage(message, bufferManager, messageOffset, maxMessageSize);

                if (MessageLogger.LogMessagesAtTransportLevel && !this.readerSessionForLoggingIsInvalid)
                {
                    if (this.isSession)
                    {
                        if (this.readerSessionForLogging == null)
                        {
                            this.readerSessionForLogging = new XmlBinaryReaderSession();
                        }
                        if (this.writerSession.HasNewStrings)
                        {
                            foreach (XmlDictionaryString str in this.writerSession.GetNewStrings())
                            {
                                this.readerSessionForLogging.Add(this.writeIdCounter++, str.Value);
                            }
                        }
                    }
                    XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(messageData.Array, messageData.Offset, messageData.Count, XD.Dictionary, XmlDictionaryReaderQuotas.Max, this.readerSessionForLogging, null);
                    MessageLogger.LogMessage(ref message, reader, MessageLoggingSource.TransportSend);
                }
                else
                {
                    this.readerSessionForLoggingIsInvalid = true;
                }
                if (this.isSession)
                {
                    return(this.AddSessionInformationToMessage(messageData, bufferManager, maxMessageSize));
                }
                this.factory.ReturnMessageWriter(sessionMessageWriter);
                return(messageData);
            }
 public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
 {
     BinaryMessageEncoderFactory.BinaryBufferedMessageWriter sessionMessageWriter;
     if (message == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
     }
     if (bufferManager == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bufferManager");
     }
     if (maxMessageSize < 0)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, System.ServiceModel.SR.GetString("ValueMustBeNonNegative")));
     }
     message.Properties.Encoder = this;
     if (this.isSession)
     {
         if (this.writerSession == null)
         {
             this.writerSession = new BinaryMessageEncoderFactory.XmlBinaryWriterSessionWithQuota(this.maxSessionSize);
             this.sessionMessageWriter = new BinaryMessageEncoderFactory.BinaryBufferedMessageWriter(this.factory.binaryVersion.Dictionary, this.writerSession);
         }
         messageOffset += 5;
     }
     if (messageOffset < 0)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageOffset", messageOffset, System.ServiceModel.SR.GetString("ValueMustBeNonNegative")));
     }
     if (messageOffset > maxMessageSize)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(System.ServiceModel.SR.GetString("MaxSentMessageSizeExceeded", new object[] { maxMessageSize })));
     }
     base.ThrowIfMismatchedMessageVersion(message);
     if (this.isSession)
     {
         sessionMessageWriter = this.sessionMessageWriter;
     }
     else
     {
         sessionMessageWriter = this.factory.TakeBufferedWriter();
     }
     ArraySegment<byte> messageData = sessionMessageWriter.WriteMessage(message, bufferManager, messageOffset, maxMessageSize);
     if (MessageLogger.LogMessagesAtTransportLevel && !this.readerSessionForLoggingIsInvalid)
     {
         if (this.isSession)
         {
             if (this.readerSessionForLogging == null)
             {
                 this.readerSessionForLogging = new XmlBinaryReaderSession();
             }
             if (this.writerSession.HasNewStrings)
             {
                 foreach (XmlDictionaryString str in this.writerSession.GetNewStrings())
                 {
                     this.readerSessionForLogging.Add(this.writeIdCounter++, str.Value);
                 }
             }
         }
         XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(messageData.Array, messageData.Offset, messageData.Count, XD.Dictionary, XmlDictionaryReaderQuotas.Max, this.readerSessionForLogging, null);
         MessageLogger.LogMessage(ref message, reader, MessageLoggingSource.TransportSend);
     }
     else
     {
         this.readerSessionForLoggingIsInvalid = true;
     }
     if (this.isSession)
     {
         return this.AddSessionInformationToMessage(messageData, bufferManager, maxMessageSize);
     }
     this.factory.ReturnMessageWriter(sessionMessageWriter);
     return messageData;
 }