protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
        {
            XmlSerializer serializer;
            MessageHeaderDescriptionTable headerDescriptionTable;
            MessageHeaderDescription unknownHeaderDescription;
            bool mustUnderstand;
            bool relay;
            string actor;
            try
            {
                if (isRequest)
                {
                    serializer = _requestMessageInfo.HeaderSerializer;
                    headerDescriptionTable = _requestMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _requestMessageInfo.UnknownHeaderDescription;
                }
                else
                {
                    serializer = _replyMessageInfo.HeaderSerializer;
                    headerDescriptionTable = _replyMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _replyMessageInfo.UnknownHeaderDescription;
                }
                if (serializer != null)
                {
                    object[] headerValues = new object[headerDescriptionTable.Count];
                    MessageHeaderOfTHelper messageHeaderOfTHelper = null;
                    int headerIndex = 0;

                    foreach (MessageHeaderDescription headerDescription in messageDescription.Headers)
                    {
                        object parameterValue = parameters[headerDescription.Index];
                        if (!headerDescription.IsUnknownHeaderCollection)
                        {
                            if (headerDescription.TypedHeader)
                            {
                                if (messageHeaderOfTHelper == null)
                                    messageHeaderOfTHelper = new MessageHeaderOfTHelper(parameters.Length);
                                headerValues[headerIndex++] = messageHeaderOfTHelper.GetContentAndSaveHeaderAttributes(parameters[headerDescription.Index], headerDescription);
                            }
                            else
                                headerValues[headerIndex++] = parameterValue;
                        }
                    }

                    MemoryStream memoryStream = new MemoryStream();
                    XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream);
                    bufferWriter.WriteStartElement("root");
                    serializer.Serialize(bufferWriter, headerValues, null);
                    bufferWriter.WriteEndElement();
                    bufferWriter.Flush();
                    XmlDocument doc = new XmlDocument();
                    memoryStream.Position = 0;
                    doc.Load(memoryStream);
                    //doc.Save(Console.Out);
                    foreach (XmlElement element in doc.DocumentElement.ChildNodes)
                    {
                        MessageHeaderDescription matchingHeaderDescription = headerDescriptionTable.Get(element.LocalName, element.NamespaceURI);
                        if (matchingHeaderDescription == null)
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                            false/*mustUnderstand*/, null/*actor*/, false/*relay*/, element));
                        else
                        {
                            if (matchingHeaderDescription.TypedHeader)
                                messageHeaderOfTHelper.GetHeaderAttributes(matchingHeaderDescription, out mustUnderstand, out relay, out actor);
                            else
                            {
                                mustUnderstand = matchingHeaderDescription.MustUnderstand;
                                relay = matchingHeaderDescription.Relay;
                                actor = matchingHeaderDescription.Actor;
                            }
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                            mustUnderstand, actor, relay, element));
                        }
                    }
                }
                if (unknownHeaderDescription != null && parameters[unknownHeaderDescription.Index] != null)
                {
                    foreach (object unknownHeader in (IEnumerable)parameters[unknownHeaderDescription.Index])
                    {
                        XmlElement element = (XmlElement)GetContentOfMessageHeaderOfT(unknownHeaderDescription, unknownHeader, out mustUnderstand, out relay, out actor);
                        if (element != null)
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                  mustUnderstand, actor, relay, element));
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                    SR.Format(SR.SFxErrorSerializingHeader, messageDescription.MessageName, e.Message), e));
            }
        }
Exemple #2
0
 protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
 {
     try
     {
         XmlSerializer headerSerializer;
         OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
         MessageHeaderDescription unknownHeaderDescription;
         bool   mustUnderstand;
         bool   relay;
         string actor;
         if (isRequest)
         {
             headerSerializer         = this.requestMessageInfo.HeaderSerializer;
             headerDescriptionTable   = this.requestMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.requestMessageInfo.UnknownHeaderDescription;
         }
         else
         {
             headerSerializer         = this.replyMessageInfo.HeaderSerializer;
             headerDescriptionTable   = this.replyMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.replyMessageInfo.UnknownHeaderDescription;
         }
         if (headerSerializer != null)
         {
             object[] o = new object[headerDescriptionTable.Count];
             MessageHeaderOfTHelper helper = null;
             int num = 0;
             foreach (MessageHeaderDescription description2 in messageDescription.Headers)
             {
                 object obj2 = parameters[description2.Index];
                 if (!description2.IsUnknownHeaderCollection)
                 {
                     if (description2.TypedHeader)
                     {
                         if (helper == null)
                         {
                             helper = new MessageHeaderOfTHelper(parameters.Length);
                         }
                         o[num++] = helper.GetContentAndSaveHeaderAttributes(parameters[description2.Index], description2);
                     }
                     else
                     {
                         o[num++] = obj2;
                     }
                 }
             }
             MemoryStream        stream    = new MemoryStream();
             XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateTextWriter(stream);
             xmlWriter.WriteStartElement("root");
             headerSerializer.Serialize(xmlWriter, o, null, this.isEncoded ? GetEncoding(message.Version.Envelope) : null);
             xmlWriter.WriteEndElement();
             xmlWriter.Flush();
             XmlDocument document = new XmlDocument();
             stream.Position = 0L;
             document.Load(stream);
             foreach (XmlElement element in document.DocumentElement.ChildNodes)
             {
                 MessageHeaderDescription headerDescription = headerDescriptionTable.Get(element.LocalName, element.NamespaceURI);
                 if (headerDescription == null)
                 {
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI, false, null, false, element));
                 }
                 else
                 {
                     if (headerDescription.TypedHeader)
                     {
                         helper.GetHeaderAttributes(headerDescription, out mustUnderstand, out relay, out actor);
                     }
                     else
                     {
                         mustUnderstand = headerDescription.MustUnderstand;
                         relay          = headerDescription.Relay;
                         actor          = headerDescription.Actor;
                     }
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI, mustUnderstand, actor, relay, element));
                 }
             }
         }
         if ((unknownHeaderDescription != null) && (parameters[unknownHeaderDescription.Index] != null))
         {
             foreach (object obj3 in (IEnumerable)parameters[unknownHeaderDescription.Index])
             {
                 XmlElement headerValue = (XmlElement)OperationFormatter.GetContentOfMessageHeaderOfT(unknownHeaderDescription, obj3, out mustUnderstand, out relay, out actor);
                 if (headerValue != null)
                 {
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, headerValue.LocalName, headerValue.NamespaceURI, mustUnderstand, actor, relay, headerValue));
                 }
             }
         }
     }
     catch (InvalidOperationException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("SFxErrorSerializingHeader", new object[] { messageDescription.MessageName, exception.Message }), exception));
     }
 }
        protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
        {
            try
            {
                XmlSerializer serializer;
                MessageHeaderDescriptionTable headerDescriptionTable;
                MessageHeaderDescription unknownHeaderDescription;
                if (isRequest)
                {
                    serializer = _requestMessageInfo.HeaderSerializer;
                    headerDescriptionTable = _requestMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _requestMessageInfo.UnknownHeaderDescription;
                }
                else
                {
                    serializer = _replyMessageInfo.HeaderSerializer;
                    headerDescriptionTable = _replyMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _replyMessageInfo.UnknownHeaderDescription;
                }
                MessageHeaders headers = message.Headers;
                ArrayList unknownHeaders = null;
                XmlDocument xmlDoc = null;
                if (unknownHeaderDescription != null)
                {
                    unknownHeaders = new ArrayList();
                    xmlDoc = new XmlDocument();
                }
                if (serializer == null)
                {
                    if (unknownHeaderDescription != null)
                    {
                        for (int headerIndex = 0; headerIndex < headers.Count; headerIndex++)
                            AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, null/*bufferWriter*/, headers[headerIndex], headers.GetReaderAtHeader(headerIndex));
                        parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader<XmlElement>) : typeof(XmlElement));
                    }
                    return;
                }


                MemoryStream memoryStream = new MemoryStream();
                XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream);
                message.WriteStartEnvelope(bufferWriter);
                message.WriteStartHeaders(bufferWriter);
                MessageHeaderOfTHelper messageHeaderOfTHelper = null;
                for (int headerIndex = 0; headerIndex < headers.Count; headerIndex++)
                {
                    MessageHeaderInfo header = headers[headerIndex];
                    XmlDictionaryReader headerReader = headers.GetReaderAtHeader(headerIndex);
                    MessageHeaderDescription matchingHeaderDescription = headerDescriptionTable.Get(header.Name, header.Namespace);
                    if (matchingHeaderDescription != null)
                    {
                        if (header.MustUnderstand)
                            headers.UnderstoodHeaders.Add(header);
                        if (matchingHeaderDescription.TypedHeader)
                        {
                            if (messageHeaderOfTHelper == null)
                                messageHeaderOfTHelper = new MessageHeaderOfTHelper(parameters.Length);
                            messageHeaderOfTHelper.SetHeaderAttributes(matchingHeaderDescription, header.MustUnderstand, header.Relay, header.Actor);
                        }
                    }
                    if (matchingHeaderDescription == null && unknownHeaderDescription != null)
                        AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, bufferWriter, header, headerReader);
                    else
                        bufferWriter.WriteNode(headerReader, false);
                    headerReader.Dispose();
                }
                bufferWriter.WriteEndElement();
                bufferWriter.WriteEndElement();
                bufferWriter.Flush();

                /*
                XmlDocument doc = new XmlDocument();
                memoryStream.Position = 0;
                doc.Load(memoryStream);
                doc.Save(Console.Out);
                */

                memoryStream.Position = 0;
                ArraySegment<byte> memoryBuffer;
                memoryStream.TryGetBuffer(out memoryBuffer);
                XmlDictionaryReader bufferReader = XmlDictionaryReader.CreateTextReader(memoryBuffer.Array, 0, (int)memoryStream.Length, XmlDictionaryReaderQuotas.Max);

                bufferReader.ReadStartElement();
                bufferReader.MoveToContent();
                if (!bufferReader.IsEmptyElement)
                {
                    bufferReader.ReadStartElement();
                    object[] headerValues = (object[])serializer.Deserialize(bufferReader);
                    int headerIndex = 0;
                    foreach (MessageHeaderDescription headerDescription in messageDescription.Headers)
                    {
                        if (!headerDescription.IsUnknownHeaderCollection)
                        {
                            object parameterValue = headerValues[headerIndex++];
                            if (headerDescription.TypedHeader && parameterValue != null)
                                parameterValue = messageHeaderOfTHelper.CreateMessageHeader(headerDescription, parameterValue);
                            parameters[headerDescription.Index] = parameterValue;
                        }
                    }
                    bufferReader.Dispose();
                }
                if (unknownHeaderDescription != null)
                    parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader<XmlElement>) : typeof(XmlElement));
            }
            catch (InvalidOperationException e)
            {
                // all exceptions from XmlSerializer get wrapped in InvalidOperationException,
                // so we must be conservative and never turn this into a fault
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                    SR.Format(SR.SFxErrorDeserializingHeader, messageDescription.MessageName), e));
            }
        }
Exemple #4
0
 protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
 {
     try
     {
         XmlSerializer headerSerializer;
         OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
         MessageHeaderDescription unknownHeaderDescription;
         if (isRequest)
         {
             headerSerializer         = this.requestMessageInfo.HeaderSerializer;
             headerDescriptionTable   = this.requestMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.requestMessageInfo.UnknownHeaderDescription;
         }
         else
         {
             headerSerializer         = this.replyMessageInfo.HeaderSerializer;
             headerDescriptionTable   = this.replyMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.replyMessageInfo.UnknownHeaderDescription;
         }
         MessageHeaders headers        = message.Headers;
         ArrayList      unknownHeaders = null;
         XmlDocument    xmlDoc         = null;
         if (unknownHeaderDescription != null)
         {
             unknownHeaders = new ArrayList();
             xmlDoc         = new XmlDocument();
         }
         if (headerSerializer == null)
         {
             if (unknownHeaderDescription != null)
             {
                 for (int i = 0; i < headers.Count; i++)
                 {
                     AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, null, headers[i], headers.GetReaderAtHeader(i));
                 }
                 parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader <XmlElement>) : typeof(XmlElement));
             }
         }
         else
         {
             MemoryStream        stream = new MemoryStream();
             XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream);
             message.WriteStartEnvelope(writer);
             message.WriteStartHeaders(writer);
             MessageHeaderOfTHelper helper = null;
             for (int j = 0; j < headers.Count; j++)
             {
                 MessageHeaderInfo        headerInfo        = headers[j];
                 XmlDictionaryReader      readerAtHeader    = headers.GetReaderAtHeader(j);
                 MessageHeaderDescription headerDescription = headerDescriptionTable.Get(headerInfo.Name, headerInfo.Namespace);
                 if (headerDescription != null)
                 {
                     if (headerInfo.MustUnderstand)
                     {
                         headers.UnderstoodHeaders.Add(headerInfo);
                     }
                     if (headerDescription.TypedHeader)
                     {
                         if (helper == null)
                         {
                             helper = new MessageHeaderOfTHelper(parameters.Length);
                         }
                         helper.SetHeaderAttributes(headerDescription, headerInfo.MustUnderstand, headerInfo.Relay, headerInfo.Actor);
                     }
                 }
                 if ((headerDescription == null) && (unknownHeaderDescription != null))
                 {
                     AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, writer, headerInfo, readerAtHeader);
                 }
                 else
                 {
                     writer.WriteNode(readerAtHeader, false);
                 }
                 readerAtHeader.Close();
             }
             writer.WriteEndElement();
             writer.WriteEndElement();
             writer.Flush();
             stream.Position = 0L;
             XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateTextReader(stream.GetBuffer(), 0, (int)stream.Length, XmlDictionaryReaderQuotas.Max);
             xmlReader.ReadStartElement();
             xmlReader.MoveToContent();
             if (!xmlReader.IsEmptyElement)
             {
                 xmlReader.ReadStartElement();
                 object[] objArray = (object[])headerSerializer.Deserialize(xmlReader, this.isEncoded ? GetEncoding(message.Version.Envelope) : null);
                 int      num3     = 0;
                 foreach (MessageHeaderDescription description3 in messageDescription.Headers)
                 {
                     if (!description3.IsUnknownHeaderCollection)
                     {
                         object headerValue = objArray[num3++];
                         if (description3.TypedHeader && (headerValue != null))
                         {
                             headerValue = helper.CreateMessageHeader(description3, headerValue);
                         }
                         parameters[description3.Index] = headerValue;
                     }
                 }
                 xmlReader.Close();
             }
             if (unknownHeaderDescription != null)
             {
                 parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader <XmlElement>) : typeof(XmlElement));
             }
         }
     }
     catch (InvalidOperationException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("SFxErrorDeserializingHeader", new object[] { messageDescription.MessageName }), exception));
     }
 }
Exemple #5
0
        protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
        {
            XmlSerializer serializer;
            MessageHeaderDescriptionTable headerDescriptionTable;
            MessageHeaderDescription      unknownHeaderDescription;
            bool   mustUnderstand;
            bool   relay;
            string actor;

            try
            {
                if (isRequest)
                {
                    serializer               = _requestMessageInfo.HeaderSerializer;
                    headerDescriptionTable   = _requestMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _requestMessageInfo.UnknownHeaderDescription;
                }
                else
                {
                    serializer               = _replyMessageInfo.HeaderSerializer;
                    headerDescriptionTable   = _replyMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _replyMessageInfo.UnknownHeaderDescription;
                }
                if (serializer != null)
                {
                    object[] headerValues = new object[headerDescriptionTable.Count];
                    MessageHeaderOfTHelper messageHeaderOfTHelper = null;
                    int headerIndex = 0;

                    foreach (MessageHeaderDescription headerDescription in messageDescription.Headers)
                    {
                        object parameterValue = parameters[headerDescription.Index];
                        if (!headerDescription.IsUnknownHeaderCollection)
                        {
                            if (headerDescription.TypedHeader)
                            {
                                if (messageHeaderOfTHelper == null)
                                {
                                    messageHeaderOfTHelper = new MessageHeaderOfTHelper(parameters.Length);
                                }

                                headerValues[headerIndex++] = messageHeaderOfTHelper.GetContentAndSaveHeaderAttributes(parameters[headerDescription.Index], headerDescription);
                            }
                            else
                            {
                                headerValues[headerIndex++] = parameterValue;
                            }
                        }
                    }

                    MemoryStream        memoryStream = new MemoryStream();
                    XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream);
                    bufferWriter.WriteStartElement("root");
                    serializer.Serialize(bufferWriter, headerValues, null, _isEncoded ? GetEncoding(message.Version.Envelope) : null);
                    bufferWriter.WriteEndElement();
                    bufferWriter.Flush();
                    XmlDocument doc = new XmlDocument();
                    memoryStream.Position = 0;
                    doc.Load(memoryStream);
                    foreach (XmlElement element in doc.DocumentElement.ChildNodes)
                    {
                        MessageHeaderDescription matchingHeaderDescription = headerDescriptionTable.Get(element.LocalName, element.NamespaceURI);
                        if (matchingHeaderDescription == null)
                        {
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                            false /*mustUnderstand*/, null /*actor*/, false /*relay*/, element));
                        }
                        else
                        {
                            if (matchingHeaderDescription.TypedHeader)
                            {
                                messageHeaderOfTHelper.GetHeaderAttributes(matchingHeaderDescription, out mustUnderstand, out relay, out actor);
                            }
                            else
                            {
                                mustUnderstand = matchingHeaderDescription.MustUnderstand;
                                relay          = matchingHeaderDescription.Relay;
                                actor          = matchingHeaderDescription.Actor;
                            }
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                            mustUnderstand, actor, relay, element));
                        }
                    }
                }
                if (unknownHeaderDescription != null && parameters[unknownHeaderDescription.Index] != null)
                {
                    foreach (object unknownHeader in (IEnumerable)parameters[unknownHeaderDescription.Index])
                    {
                        XmlElement element = (XmlElement)GetContentOfMessageHeaderOfT(unknownHeaderDescription, unknownHeader, out mustUnderstand, out relay, out actor);
                        if (element != null)
                        {
                            message.Headers.Add(new XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI,
                                                                            mustUnderstand, actor, relay, element));
                        }
                    }
                }
            }
            catch (InvalidOperationException e)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                              SR.Format(SR.SFxErrorSerializingHeader, messageDescription.MessageName, e.Message), e));
            }
        }
Exemple #6
0
        protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
        {
            try
            {
                XmlSerializer serializer;
                MessageHeaderDescriptionTable headerDescriptionTable;
                MessageHeaderDescription      unknownHeaderDescription;
                if (isRequest)
                {
                    serializer               = _requestMessageInfo.HeaderSerializer;
                    headerDescriptionTable   = _requestMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _requestMessageInfo.UnknownHeaderDescription;
                }
                else
                {
                    serializer               = _replyMessageInfo.HeaderSerializer;
                    headerDescriptionTable   = _replyMessageInfo.HeaderDescriptionTable;
                    unknownHeaderDescription = _replyMessageInfo.UnknownHeaderDescription;
                }
                MessageHeaders headers        = message.Headers;
                ArrayList      unknownHeaders = null;
                XmlDocument    xmlDoc         = null;
                if (unknownHeaderDescription != null)
                {
                    unknownHeaders = new ArrayList();
                    xmlDoc         = new XmlDocument();
                }
                if (serializer == null)
                {
                    if (unknownHeaderDescription != null)
                    {
                        for (int headerIndex = 0; headerIndex < headers.Count; headerIndex++)
                        {
                            AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, null /*bufferWriter*/, headers[headerIndex], headers.GetReaderAtHeader(headerIndex));
                        }

                        parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader <XmlElement>) : typeof(XmlElement));
                    }
                    return;
                }


                MemoryStream        memoryStream = new MemoryStream();
                XmlDictionaryWriter bufferWriter = XmlDictionaryWriter.CreateTextWriter(memoryStream);
                message.WriteStartEnvelope(bufferWriter);
                message.WriteStartHeaders(bufferWriter);
                MessageHeaderOfTHelper messageHeaderOfTHelper = null;
                for (int headerIndex = 0; headerIndex < headers.Count; headerIndex++)
                {
                    MessageHeaderInfo        header       = headers[headerIndex];
                    XmlDictionaryReader      headerReader = headers.GetReaderAtHeader(headerIndex);
                    MessageHeaderDescription matchingHeaderDescription = headerDescriptionTable.Get(header.Name, header.Namespace);
                    if (matchingHeaderDescription != null)
                    {
                        if (header.MustUnderstand)
                        {
                            headers.UnderstoodHeaders.Add(header);
                        }

                        if (matchingHeaderDescription.TypedHeader)
                        {
                            if (messageHeaderOfTHelper == null)
                            {
                                messageHeaderOfTHelper = new MessageHeaderOfTHelper(parameters.Length);
                            }

                            messageHeaderOfTHelper.SetHeaderAttributes(matchingHeaderDescription, header.MustUnderstand, header.Relay, header.Actor);
                        }
                    }
                    if (matchingHeaderDescription == null && unknownHeaderDescription != null)
                    {
                        AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, bufferWriter, header, headerReader);
                    }
                    else
                    {
                        bufferWriter.WriteNode(headerReader, false);
                    }

                    headerReader.Dispose();
                }
                bufferWriter.WriteEndElement();
                bufferWriter.WriteEndElement();
                bufferWriter.Flush();

                /*
                 * XmlDocument doc = new XmlDocument();
                 * memoryStream.Position = 0;
                 * doc.Load(memoryStream);
                 * doc.Save(Console.Out);
                 */

                memoryStream.Position = 0;
                ArraySegment <byte> memoryBuffer;
                memoryStream.TryGetBuffer(out memoryBuffer);
                XmlDictionaryReader bufferReader = XmlDictionaryReader.CreateTextReader(memoryBuffer.Array, 0, (int)memoryStream.Length, XmlDictionaryReaderQuotas.Max);

                bufferReader.ReadStartElement();
                bufferReader.MoveToContent();
                if (!bufferReader.IsEmptyElement)
                {
                    bufferReader.ReadStartElement();
                    object[] headerValues = (object[])serializer.Deserialize(bufferReader, _isEncoded ? GetEncoding(message.Version.Envelope) : null);
                    int      headerIndex  = 0;
                    foreach (MessageHeaderDescription headerDescription in messageDescription.Headers)
                    {
                        if (!headerDescription.IsUnknownHeaderCollection)
                        {
                            object parameterValue = headerValues[headerIndex++];
                            if (headerDescription.TypedHeader && parameterValue != null)
                            {
                                parameterValue = messageHeaderOfTHelper.CreateMessageHeader(headerDescription, parameterValue);
                            }

                            parameters[headerDescription.Index] = parameterValue;
                        }
                    }
                    bufferReader.Dispose();
                }
                if (unknownHeaderDescription != null)
                {
                    parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader <XmlElement>) : typeof(XmlElement));
                }
            }
            catch (InvalidOperationException e)
            {
                // all exceptions from XmlSerializer get wrapped in InvalidOperationException,
                // so we must be conservative and never turn this into a fault
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                              SR.Format(SR.SFxErrorDeserializingHeader, messageDescription.MessageName), e));
            }
        }
 protected override void AddHeadersToMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
 {
     try
     {
         XmlSerializer headerSerializer;
         OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
         MessageHeaderDescription unknownHeaderDescription;
         bool mustUnderstand;
         bool relay;
         string actor;
         if (isRequest)
         {
             headerSerializer = this.requestMessageInfo.HeaderSerializer;
             headerDescriptionTable = this.requestMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.requestMessageInfo.UnknownHeaderDescription;
         }
         else
         {
             headerSerializer = this.replyMessageInfo.HeaderSerializer;
             headerDescriptionTable = this.replyMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.replyMessageInfo.UnknownHeaderDescription;
         }
         if (headerSerializer != null)
         {
             object[] o = new object[headerDescriptionTable.Count];
             MessageHeaderOfTHelper helper = null;
             int num = 0;
             foreach (MessageHeaderDescription description2 in messageDescription.Headers)
             {
                 object obj2 = parameters[description2.Index];
                 if (!description2.IsUnknownHeaderCollection)
                 {
                     if (description2.TypedHeader)
                     {
                         if (helper == null)
                         {
                             helper = new MessageHeaderOfTHelper(parameters.Length);
                         }
                         o[num++] = helper.GetContentAndSaveHeaderAttributes(parameters[description2.Index], description2);
                     }
                     else
                     {
                         o[num++] = obj2;
                     }
                 }
             }
             MemoryStream stream = new MemoryStream();
             XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateTextWriter(stream);
             xmlWriter.WriteStartElement("root");
             headerSerializer.Serialize(xmlWriter, o, null, this.isEncoded ? GetEncoding(message.Version.Envelope) : null);
             xmlWriter.WriteEndElement();
             xmlWriter.Flush();
             XmlDocument document = new XmlDocument();
             stream.Position = 0L;
             document.Load(stream);
             foreach (XmlElement element in document.DocumentElement.ChildNodes)
             {
                 MessageHeaderDescription headerDescription = headerDescriptionTable.Get(element.LocalName, element.NamespaceURI);
                 if (headerDescription == null)
                 {
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI, false, null, false, element));
                 }
                 else
                 {
                     if (headerDescription.TypedHeader)
                     {
                         helper.GetHeaderAttributes(headerDescription, out mustUnderstand, out relay, out actor);
                     }
                     else
                     {
                         mustUnderstand = headerDescription.MustUnderstand;
                         relay = headerDescription.Relay;
                         actor = headerDescription.Actor;
                     }
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, element.LocalName, element.NamespaceURI, mustUnderstand, actor, relay, element));
                 }
             }
         }
         if ((unknownHeaderDescription != null) && (parameters[unknownHeaderDescription.Index] != null))
         {
             foreach (object obj3 in (IEnumerable) parameters[unknownHeaderDescription.Index])
             {
                 XmlElement headerValue = (XmlElement) OperationFormatter.GetContentOfMessageHeaderOfT(unknownHeaderDescription, obj3, out mustUnderstand, out relay, out actor);
                 if (headerValue != null)
                 {
                     message.Headers.Add(new OperationFormatter.XmlElementMessageHeader(this, message.Version, headerValue.LocalName, headerValue.NamespaceURI, mustUnderstand, actor, relay, headerValue));
                 }
             }
         }
     }
     catch (InvalidOperationException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("SFxErrorSerializingHeader", new object[] { messageDescription.MessageName, exception.Message }), exception));
     }
 }
 protected override void GetHeadersFromMessage(Message message, MessageDescription messageDescription, object[] parameters, bool isRequest)
 {
     try
     {
         XmlSerializer headerSerializer;
         OperationFormatter.MessageHeaderDescriptionTable headerDescriptionTable;
         MessageHeaderDescription unknownHeaderDescription;
         if (isRequest)
         {
             headerSerializer = this.requestMessageInfo.HeaderSerializer;
             headerDescriptionTable = this.requestMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.requestMessageInfo.UnknownHeaderDescription;
         }
         else
         {
             headerSerializer = this.replyMessageInfo.HeaderSerializer;
             headerDescriptionTable = this.replyMessageInfo.HeaderDescriptionTable;
             unknownHeaderDescription = this.replyMessageInfo.UnknownHeaderDescription;
         }
         MessageHeaders headers = message.Headers;
         ArrayList unknownHeaders = null;
         XmlDocument xmlDoc = null;
         if (unknownHeaderDescription != null)
         {
             unknownHeaders = new ArrayList();
             xmlDoc = new XmlDocument();
         }
         if (headerSerializer == null)
         {
             if (unknownHeaderDescription != null)
             {
                 for (int i = 0; i < headers.Count; i++)
                 {
                     AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, null, headers[i], headers.GetReaderAtHeader(i));
                 }
                 parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader<XmlElement>) : typeof(XmlElement));
             }
         }
         else
         {
             MemoryStream stream = new MemoryStream();
             XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream);
             message.WriteStartEnvelope(writer);
             message.WriteStartHeaders(writer);
             MessageHeaderOfTHelper helper = null;
             for (int j = 0; j < headers.Count; j++)
             {
                 MessageHeaderInfo headerInfo = headers[j];
                 XmlDictionaryReader readerAtHeader = headers.GetReaderAtHeader(j);
                 MessageHeaderDescription headerDescription = headerDescriptionTable.Get(headerInfo.Name, headerInfo.Namespace);
                 if (headerDescription != null)
                 {
                     if (headerInfo.MustUnderstand)
                     {
                         headers.UnderstoodHeaders.Add(headerInfo);
                     }
                     if (headerDescription.TypedHeader)
                     {
                         if (helper == null)
                         {
                             helper = new MessageHeaderOfTHelper(parameters.Length);
                         }
                         helper.SetHeaderAttributes(headerDescription, headerInfo.MustUnderstand, headerInfo.Relay, headerInfo.Actor);
                     }
                 }
                 if ((headerDescription == null) && (unknownHeaderDescription != null))
                 {
                     AddUnknownHeader(unknownHeaderDescription, unknownHeaders, xmlDoc, writer, headerInfo, readerAtHeader);
                 }
                 else
                 {
                     writer.WriteNode(readerAtHeader, false);
                 }
                 readerAtHeader.Close();
             }
             writer.WriteEndElement();
             writer.WriteEndElement();
             writer.Flush();
             stream.Position = 0L;
             XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateTextReader(stream.GetBuffer(), 0, (int) stream.Length, XmlDictionaryReaderQuotas.Max);
             xmlReader.ReadStartElement();
             xmlReader.MoveToContent();
             if (!xmlReader.IsEmptyElement)
             {
                 xmlReader.ReadStartElement();
                 object[] objArray = (object[]) headerSerializer.Deserialize(xmlReader, this.isEncoded ? GetEncoding(message.Version.Envelope) : null);
                 int num3 = 0;
                 foreach (MessageHeaderDescription description3 in messageDescription.Headers)
                 {
                     if (!description3.IsUnknownHeaderCollection)
                     {
                         object headerValue = objArray[num3++];
                         if (description3.TypedHeader && (headerValue != null))
                         {
                             headerValue = helper.CreateMessageHeader(description3, headerValue);
                         }
                         parameters[description3.Index] = headerValue;
                     }
                 }
                 xmlReader.Close();
             }
             if (unknownHeaderDescription != null)
             {
                 parameters[unknownHeaderDescription.Index] = unknownHeaders.ToArray(unknownHeaderDescription.TypedHeader ? typeof(MessageHeader<XmlElement>) : typeof(XmlElement));
             }
         }
     }
     catch (InvalidOperationException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("SFxErrorDeserializingHeader", new object[] { messageDescription.MessageName }), exception));
     }
 }