private void GenerateHeaders(CodeAttributeDeclarationCollection metadata, SoapBindingUse use, bool rpc, MessageBinding requestMessage, MessageBinding responseMessage)
 {
     Hashtable hashtable = new Hashtable();
     for (int i = 0; i < 2; i++)
     {
         MessageBinding binding;
         SoapHeaderDirection @in;
         if (i == 0)
         {
             binding = requestMessage;
             @in = SoapHeaderDirection.In;
         }
         else
         {
             if (responseMessage == null)
             {
                 continue;
             }
             binding = responseMessage;
             @in = SoapHeaderDirection.Out;
         }
         SoapHeaderBinding[] bindingArray = (SoapHeaderBinding[]) binding.Extensions.FindAll(typeof(SoapHeaderBinding));
         foreach (SoapHeaderBinding binding2 in bindingArray)
         {
             if (binding2.MapToProperty)
             {
                 XmlTypeMapping mapping;
                 string str;
                 if (use != binding2.Use)
                 {
                     throw new InvalidOperationException(System.Web.Services.Res.GetString("WebDescriptionHeaderAndBodyUseMismatch"));
                 }
                 if ((use == SoapBindingUse.Encoded) && !this.IsSoapEncodingPresent(binding2.Encoding))
                 {
                     throw new InvalidOperationException(System.Web.Services.Res.GetString("WebUnknownEncodingStyle", new object[] { binding2.Encoding }));
                 }
                 Message message = base.ServiceDescriptions.GetMessage(binding2.Message);
                 if (message == null)
                 {
                     throw new InvalidOperationException(System.Web.Services.Res.GetString("MissingMessage2", new object[] { binding2.Message.Name, binding2.Message.Namespace }));
                 }
                 MessagePart part = message.FindPartByName(binding2.Part);
                 if (part == null)
                 {
                     throw new InvalidOperationException(System.Web.Services.Res.GetString("MissingMessagePartForMessageFromNamespace3", new object[] { part.Name, binding2.Message.Name, binding2.Message.Namespace }));
                 }
                 if (use == SoapBindingUse.Encoded)
                 {
                     if (part.Type.IsEmpty)
                     {
                         throw new InvalidOperationException(System.Web.Services.Res.GetString("WebDescriptionPartTypeRequired", new object[] { part.Name, binding2.Message.Name, binding2.Message.Namespace }));
                     }
                     if (!part.Element.IsEmpty)
                     {
                         base.UnsupportedOperationBindingWarning(System.Web.Services.Res.GetString("WebDescriptionPartElementWarning", new object[] { part.Name, binding2.Message.Name, binding2.Message.Namespace }));
                     }
                     mapping = this.soapImporter.ImportDerivedTypeMapping(part.Type, typeof(SoapHeader), true);
                     str = "type=" + part.Type.ToString();
                 }
                 else
                 {
                     if (part.Element.IsEmpty)
                     {
                         throw new InvalidOperationException(System.Web.Services.Res.GetString("WebDescriptionPartElementRequired", new object[] { part.Name, binding2.Message.Name, binding2.Message.Namespace }));
                     }
                     if (!part.Type.IsEmpty)
                     {
                         base.UnsupportedOperationBindingWarning(System.Web.Services.Res.GetString("WebDescriptionPartTypeWarning", new object[] { part.Name, binding2.Message.Name, binding2.Message.Namespace }));
                     }
                     mapping = this.xmlImporter.ImportDerivedTypeMapping(part.Element, typeof(SoapHeader), true);
                     str = "element=" + part.Element.ToString();
                 }
                 LocalSoapHeader header = (LocalSoapHeader) hashtable[str];
                 if (header == null)
                 {
                     GlobalSoapHeader header2 = (GlobalSoapHeader) this.classHeaders[str];
                     if (header2 == null)
                     {
                         header2 = new GlobalSoapHeader {
                             isEncoded = use == SoapBindingUse.Encoded
                         };
                         string identifier = CodeIdentifier.MakeValid(mapping.ElementName);
                         if (identifier == mapping.TypeName)
                         {
                             identifier = identifier + "Value";
                         }
                         identifier = base.MethodNames.AddUnique(identifier, mapping);
                         header2.fieldName = identifier;
                         WebCodeGenerator.AddMember(base.CodeTypeDeclaration, mapping.TypeFullName, header2.fieldName, null, null, CodeFlags.IsPublic, base.ServiceImporter.CodeGenerationOptions);
                         header2.mapping = mapping;
                         this.classHeaders.Add(str, header2);
                         if (this.headers[str] == null)
                         {
                             this.headers.Add(str, header2);
                         }
                     }
                     header = new LocalSoapHeader {
                         fieldName = header2.fieldName,
                         direction = @in
                     };
                     hashtable.Add(str, header);
                 }
                 else if (header.direction != @in)
                 {
                     header.direction = SoapHeaderDirection.InOut;
                 }
             }
         }
     }
     foreach (LocalSoapHeader header3 in hashtable.Values)
     {
         this.BeginMetadata();
         if (header3.direction == SoapHeaderDirection.Out)
         {
             this.AddMetadataProperty("Direction", (CodeExpression) new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapHeaderDirection).FullName), SoapHeaderDirection.Out.ToString()));
         }
         else if (header3.direction == SoapHeaderDirection.InOut)
         {
             this.AddMetadataProperty("Direction", (CodeExpression) new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapHeaderDirection).FullName), SoapHeaderDirection.InOut.ToString()));
         }
         this.EndMetadata(metadata, typeof(SoapHeaderAttribute), header3.fieldName);
     }
 }
        void GenerateHeaders(CodeAttributeDeclarationCollection metadata, SoapBindingUse use, bool rpc, MessageBinding requestMessage, MessageBinding responseMessage) { 
            Hashtable localHeaders = new Hashtable();

            for (int i = 0; i < 2; ++i) {
                MessageBinding messageBinding;
                SoapHeaderDirection direction;
                if (i == 0) {
                    messageBinding = requestMessage;
                    direction = SoapHeaderDirection.In;
                }
                else if (responseMessage != null) {
                    messageBinding = responseMessage;
                    direction = SoapHeaderDirection.Out;
                }
                else
                    continue;

                SoapHeaderBinding[] headerBindings = (SoapHeaderBinding[])messageBinding.Extensions.FindAll(typeof(SoapHeaderBinding));
                foreach (SoapHeaderBinding header in headerBindings) {
                    // Skip headers which should not be mapped to properties (extension importers can control this)
                    if (!header.MapToProperty) continue;

                    if (use != header.Use) throw new InvalidOperationException(Res.GetString(Res.WebDescriptionHeaderAndBodyUseMismatch));
                    if (use == SoapBindingUse.Encoded && !IsSoapEncodingPresent(header.Encoding) )
                        throw new InvalidOperationException(Res.GetString(Res.WebUnknownEncodingStyle, header.Encoding));

                    Message message = ServiceDescriptions.GetMessage(header.Message);
                    if (message == null) throw new InvalidOperationException(Res.GetString(Res.MissingMessage2, header.Message.Name, header.Message.Namespace));

                    MessagePart part = message.FindPartByName(header.Part);
                    if (part == null) throw new InvalidOperationException(Res.GetString(Res.MissingMessagePartForMessageFromNamespace3, part.Name, header.Message.Name, header.Message.Namespace));

                    XmlTypeMapping mapping;
                    string key;
                    if (use == SoapBindingUse.Encoded) {
                        if (part.Type.IsEmpty) throw new InvalidOperationException(Res.GetString(Res.WebDescriptionPartTypeRequired, part.Name, header.Message.Name, header.Message.Namespace));
                        if (!part.Element.IsEmpty) UnsupportedOperationBindingWarning(Res.GetString(Res.WebDescriptionPartElementWarning, part.Name, header.Message.Name, header.Message.Namespace));
                        mapping = soapImporter.ImportDerivedTypeMapping(part.Type, typeof(SoapHeader), true);
                        key = "type=" + part.Type.ToString();
                    }
                    else {
                        if (part.Element.IsEmpty) throw new InvalidOperationException(Res.GetString(Res.WebDescriptionPartElementRequired, part.Name, header.Message.Name, header.Message.Namespace));
                        if (!part.Type.IsEmpty) UnsupportedOperationBindingWarning(Res.GetString(Res.WebDescriptionPartTypeWarning, part.Name, header.Message.Name, header.Message.Namespace));
                        mapping = xmlImporter.ImportDerivedTypeMapping(part.Element, typeof(SoapHeader), true);
                        key = "element=" + part.Element.ToString();
                    }
                    LocalSoapHeader localHeader = (LocalSoapHeader)localHeaders[key];
                    if (localHeader == null) {
                        GlobalSoapHeader globalHeader = (GlobalSoapHeader)classHeaders[key];
                        if (globalHeader == null) {
                            globalHeader = new GlobalSoapHeader();
                            globalHeader.isEncoded = use == SoapBindingUse.Encoded;
                            string fieldName = CodeIdentifier.MakeValid(mapping.ElementName);
                            if (fieldName == mapping.TypeName) fieldName += "Value";
                            fieldName = MethodNames.AddUnique(fieldName, mapping);
                            globalHeader.fieldName = fieldName;
                            WebCodeGenerator.AddMember(CodeTypeDeclaration, mapping.TypeFullName, globalHeader.fieldName, null, null, CodeFlags.IsPublic, ServiceImporter.CodeGenerationOptions);
                            globalHeader.mapping = mapping;
                            classHeaders.Add(key, globalHeader);
                            if (headers[key] == null)
                                headers.Add(key, globalHeader);
                        }

                        localHeader = new LocalSoapHeader();
                        localHeader.fieldName = globalHeader.fieldName;
                        localHeader.direction = direction;
                        localHeaders.Add(key, localHeader);
                    }
                    else {
                        if (localHeader.direction != direction)
                            localHeader.direction = SoapHeaderDirection.InOut;
                    }
                }
            }

            foreach (LocalSoapHeader soapHeader in localHeaders.Values) {
                BeginMetadata();
                if (soapHeader.direction == SoapHeaderDirection.Out) {
                    AddMetadataProperty("Direction", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapHeaderDirection).FullName), SoapHeaderDirection.Out.ToString()));
                }
                else if (soapHeader.direction == SoapHeaderDirection.InOut) { 
                    AddMetadataProperty("Direction", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapHeaderDirection).FullName), SoapHeaderDirection.InOut.ToString()));
                }

                EndMetadata(metadata, typeof(SoapHeaderAttribute), soapHeader.fieldName);
            }
        }