public static void GetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client)
 {
     if ((mappings != null) && (mappings.Length != 0))
     {
         for (int i = 0; i < mappings.Length; i++)
         {
             SoapHeaderMapping mapping = mappings[i];
             if ((mapping.direction & direction) != 0)
             {
                 object obj2 = MemberHelper.GetValue(mapping.memberInfo, target);
                 if (mapping.repeats)
                 {
                     object[] objArray = (object[]) obj2;
                     if (objArray != null)
                     {
                         for (int j = 0; j < objArray.Length; j++)
                         {
                             if (objArray[j] != null)
                             {
                                 headers.Add((SoapHeader) objArray[j]);
                             }
                         }
                     }
                 }
                 else if (obj2 != null)
                 {
                     headers.Add((SoapHeader) obj2);
                 }
             }
         }
     }
 }
Esempio n. 2
0
		public static void SetHeaderMembers (
			SoapHeaderCollection headers,
			object target,
			SoapHeaderMapping [] mappings,
			SoapHeaderDirection direction,
			bool client)
		{
			throw new NotImplementedException ();
		}
Esempio n. 3
0
		public static void WriteHeaders (
			XmlWriter writer,
			XmlSerializer serializer,
			SoapHeaderCollection headers,
			SoapHeaderMapping [] mappings,
			SoapHeaderDirection direction,
			bool isEncoded,
			string defaultNS,
			bool serviceDefaultIsEncoded,
			string envelopeNS)
		{
			throw new NotImplementedException ();
		}
 private static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction)
 {
     if ((mappings != null) && (mappings.Length != 0))
     {
         Type c = header.GetType();
         for (int i = 0; i < mappings.Length; i++)
         {
             SoapHeaderMapping mapping = mappings[i];
             if ((((mapping.direction & direction) != 0) && mapping.custom) && mapping.headerType.IsAssignableFrom(c))
             {
                 return i;
             }
         }
     }
     return -1;
 }
Esempio n. 5
0
		internal SoapHeaderMapping (MemberInfo member, SoapHeaderAttribute attributeInfo)
		{
			this.member = member;
			direction = attributeInfo.Direction;
			if (member is PropertyInfo)
				header_type = ((PropertyInfo) member).PropertyType;
			else
				header_type = ((FieldInfo) member).FieldType;
			
			if (HeaderType == typeof(SoapHeader) || HeaderType == typeof(SoapUnknownHeader) ||
				HeaderType == typeof(SoapHeader[]) || HeaderType == typeof(SoapUnknownHeader[]))
			{
				is_unknown_header = true;
			}
			else if (!typeof(SoapHeader).IsAssignableFrom (HeaderType))
				throw new InvalidOperationException (string.Format ("Header members type must be a SoapHeader subclass"));
		}
Esempio n. 6
0
 static int FindMapping(SoapHeaderMapping[] mappings, SoapHeader header, SoapHeaderDirection direction) {
     if (mappings == null || mappings.Length == 0) return -1;
     Type headerType = header.GetType();
     for (int i = 0; i < mappings.Length; i++) {
         SoapHeaderMapping mapping = mappings[i];
         if ((mapping.direction & direction) == 0) continue;
         if (!mapping.custom) continue;
         if (mapping.headerType.IsAssignableFrom(headerType)) {
             return i;
         }
     }
     return -1;
 }
Esempio n. 7
0
		public static void WriteSoapMessage (XmlTextWriter xtw, SoapMethodStubInfo method, SoapHeaderDirection dir, object bodyContent, SoapHeaderCollection headers, bool soap12)
		{
			SoapBindingUse methodUse = dir == SoapHeaderDirection.Fault ? SoapBindingUse.Literal : method.Use;
			XmlSerializer bodySerializer = method.GetBodySerializer (dir, soap12);
			XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
			object[] headerArray = method.GetHeaderValueArray (dir, headers);
			WriteSoapMessage (xtw, methodUse, bodySerializer, headerSerializer, bodyContent, headerArray, soap12);
		}
Esempio n. 8
0
		public object[] GetHeaderValueArray (SoapHeaderDirection dir, SoapHeaderCollection headers)
		{
			HeaderInfo[] headerInfos = GetHeaders (dir);
			if (headerInfos == null) return null;

			object[] hs = new object [headerInfos.Length];
			
			for (int n=0; n<headers.Count; n++) {
				SoapHeader h = headers[n];
				Type t = h.GetType();
				for (int i=0; i<headerInfos.Length; i++)
					if (headerInfos [i].HeaderType == t)
						hs [i] = h;
			}
			return hs;
		}
Esempio n. 9
0
		public XmlSerializer GetHeaderSerializer (SoapHeaderDirection dir)
		{
			switch (dir) {
				case SoapHeaderDirection.In: return RequestHeadersSerializer;
				case SoapHeaderDirection.Out: return ResponseHeadersSerializer;
				case SoapHeaderDirection.Fault: return FaultHeadersSerializer;
				default: return null;
			}
		}
 public static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS)
 {
     if (headers.Count != 0)
     {
         SoapProtocolVersion version;
         string str;
         writer.WriteStartElement("Header", envelopeNS);
         if (envelopeNS == "http://www.w3.org/2003/05/soap-envelope")
         {
             version = SoapProtocolVersion.Soap12;
             str = "http://www.w3.org/2003/05/soap-encoding";
         }
         else
         {
             version = SoapProtocolVersion.Soap11;
             str = "http://schemas.xmlsoap.org/soap/encoding/";
         }
         int num = 0;
         ArrayList list = new ArrayList();
         SoapHeader[] o = new SoapHeader[mappings.Length];
         bool[] flagArray = new bool[o.Length];
         for (int i = 0; i < headers.Count; i++)
         {
             SoapHeader header = headers[i];
             if (header != null)
             {
                 header.version = version;
                 if (header is SoapUnknownHeader)
                 {
                     list.Add(header);
                     num++;
                 }
                 else
                 {
                     int num3;
                     if (((num3 = FindMapping(mappings, header, direction)) >= 0) && !flagArray[num3])
                     {
                         o[num3] = header;
                         flagArray[num3] = true;
                     }
                     else
                     {
                         list.Add(header);
                     }
                 }
             }
         }
         int num4 = list.Count - num;
         if (isEncoded && (num4 > 0))
         {
             SoapHeader[] array = new SoapHeader[mappings.Length + num4];
             o.CopyTo(array, 0);
             int length = mappings.Length;
             for (int k = 0; k < list.Count; k++)
             {
                 if (!(list[k] is SoapUnknownHeader))
                 {
                     array[length++] = (SoapHeader) list[k];
                 }
             }
             o = array;
         }
         TraceMethod caller = Tracing.On ? new TraceMethod(typeof(SoapHeaderHandling), "WriteHeaders", new object[0]) : null;
         if (Tracing.On)
         {
             object[] args = new object[5];
             args[0] = writer;
             args[1] = o;
             args[3] = isEncoded ? str : null;
             args[4] = "h_";
             Tracing.Enter(Tracing.TraceId("TraceWriteHeaders"), caller, new TraceMethod(serializer, "Serialize", args));
         }
         serializer.Serialize(writer, o, null, isEncoded ? str : null, "h_");
         if (Tracing.On)
         {
             Tracing.Exit(Tracing.TraceId("TraceWriteHeaders"), caller);
         }
         foreach (SoapHeader header2 in list)
         {
             if (header2 is SoapUnknownHeader)
             {
                 SoapUnknownHeader header3 = (SoapUnknownHeader) header2;
                 if (header3.Element != null)
                 {
                     header3.Element.WriteTo(writer);
                 }
             }
             else if (!isEncoded)
             {
                 string literalNamespace = SoapReflector.GetLiteralNamespace(defaultNS, serviceDefaultIsEncoded);
                 XmlSerializer target = new XmlSerializer(header2.GetType(), literalNamespace);
                 if (Tracing.On)
                 {
                     Tracing.Enter(Tracing.TraceId("TraceWriteHeaders"), caller, new TraceMethod(target, "Serialize", new object[] { writer, header2 }));
                 }
                 target.Serialize(writer, header2);
                 if (Tracing.On)
                 {
                     Tracing.Exit(Tracing.TraceId("TraceWriteHeaders"), caller);
                 }
             }
         }
         for (int j = 0; j < headers.Count; j++)
         {
             SoapHeader header4 = headers[j];
             if (header4 != null)
             {
                 header4.version = SoapProtocolVersion.Default;
             }
         }
         writer.WriteEndElement();
         writer.Flush();
     }
 }
 public string ReadHeaders(XmlReader reader, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, string envelopeNS, string encodingStyle, bool checkRequiredHeaders)
 {
     string headerElementName = null;
     reader.MoveToContent();
     if (!reader.IsStartElement("Header", envelopeNS))
     {
         if ((checkRequiredHeaders && (mappings != null)) && (mappings.Length > 0))
         {
             headerElementName = GetHeaderElementName(mappings[0].headerType);
         }
         return headerElementName;
     }
     if (reader.IsEmptyElement)
     {
         reader.Skip();
         return headerElementName;
     }
     this.unknownHeaders = new SoapHeaderCollection();
     this.unreferencedHeaders = new SoapHeaderCollection();
     this.currentThread = Thread.CurrentThread.GetHashCode();
     this.envelopeNS = envelopeNS;
     int depth = reader.Depth;
     reader.ReadStartElement();
     reader.MoveToContent();
     XmlDeserializationEvents events = new XmlDeserializationEvents {
         OnUnknownElement = new XmlElementEventHandler(this.OnUnknownElement),
         OnUnreferencedObject = new UnreferencedObjectEventHandler(this.OnUnreferencedObject)
     };
     TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadHeaders", new object[0]) : null;
     if (Tracing.On)
     {
         Tracing.Enter(Tracing.TraceId("TraceReadHeaders"), caller, new TraceMethod(serializer, "Deserialize", new object[] { reader, encodingStyle }));
     }
     object[] objArray = (object[]) serializer.Deserialize(reader, encodingStyle, events);
     if (Tracing.On)
     {
         Tracing.Exit(Tracing.TraceId("TraceReadHeaders"), caller);
     }
     for (int i = 0; i < objArray.Length; i++)
     {
         if (objArray[i] != null)
         {
             SoapHeader header = (SoapHeader) objArray[i];
             header.DidUnderstand = true;
             headers.Add(header);
         }
         else if (checkRequiredHeaders && (headerElementName == null))
         {
             headerElementName = GetHeaderElementName(mappings[i].headerType);
         }
     }
     this.currentThread = 0;
     this.envelopeNS = null;
     foreach (SoapHeader header2 in this.unreferencedHeaders)
     {
         headers.Add(header2);
     }
     this.unreferencedHeaders = null;
     foreach (SoapHeader header3 in this.unknownHeaders)
     {
         headers.Add(header3);
     }
     this.unknownHeaders = null;
     while ((depth < reader.Depth) && reader.Read())
     {
     }
     if (reader.NodeType == XmlNodeType.EndElement)
     {
         reader.Read();
     }
     return headerElementName;
 }
 public static void WriteHeaders(System.Xml.XmlWriter writer, System.Xml.Serialization.XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS)
 {
 }
 public static void SetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client)
 {
 }
Esempio n. 14
0
		public string ReadHeaders (
			XmlReader reader,
			XmlSerializer serializer,
			SoapHeaderCollection headers,
			SoapHeaderMapping [] mappings,
			SoapHeaderDirection direction,
			string envelopeNS,
			string encodingStyle,
			bool checkRequiredHeaders)
		{
			throw new NotImplementedException ();
		}
Esempio n. 15
0
		void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
		{
			Message msg = ServiceDescriptions.GetMessage (hb.Message);
			if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
			MessagePart part = msg.Parts [hb.Part];
			if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);

			XmlTypeMapping map;
			string hname;
			if (hb.Use == SoapBindingUse.Literal)
			{
				map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
				hname = part.Element.Name;
				xmlExporter.ExportTypeMapping (map);
			}
			else
			{
				map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
				hname = part.Type.Name;
				soapExporter.ExportTypeMapping (map);
			}

			string varName = headerVariables [map] as string;
			if (varName == null) 
			{
				if (hname == map.TypeName)
					varName = memberIds.AddUnique(CodeIdentifier.MakeValid (hname + "Value"),hb);
				else
					varName = memberIds.AddUnique(CodeIdentifier.MakeValid (hname),hb);
				
				string propName = varName;
				varName = varName + "Field";
				headerVariables.Add (map, varName);
				CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
				CodeTypeDeclaration.Members.Add (codeField);
				
				codeField.Attributes = MemberAttributes.Private;
				CodeMemberProperty codeProperty = new CodeMemberProperty ();
				codeProperty.Name = propName;
				codeProperty.Type = new CodeTypeReference (map.TypeFullName);
				codeProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
				codeProperty.HasGet = codeProperty.HasSet = true;
				CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), varName);
				codeProperty.SetStatements.Add (new CodeAssignStatement (ce, new CodePropertySetValueReferenceExpression()));
				codeProperty.GetStatements.Add (new CodeMethodReturnStatement (ce));
				CodeTypeDeclaration.Members.Add (codeProperty);

				varName = propName;
			}
			
			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
			att.Arguments.Add (GetArg (varName));
#if ONLY_1_0
			att.Arguments.Add (GetArg ("Required", false));
#endif
			if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
			AddCustomAttribute (method, att, true);
		}
Esempio n. 16
0
		public static void ReadSoapMessage (XmlTextReader xmlReader, SoapMethodStubInfo method, SoapHeaderDirection dir, bool soap12, out object body, out SoapHeaderCollection headers)
		{
			XmlSerializer bodySerializer = method.GetBodySerializer (dir, false);// no need to worry about soap12 arg since no call for Fault anyways here.
			XmlSerializer headerSerializer = method.GetHeaderSerializer (dir);
			ReadSoapMessage (xmlReader, bodySerializer, headerSerializer, soap12, out body, out headers);
		}
		internal void CollectHeaders (object target, HeaderInfo[] headers, SoapHeaderDirection direction)
		{
			Headers.Clear ();
			foreach (HeaderInfo hi in headers) 
			{
				if ((hi.Direction & direction) != 0 && !hi.Custom) 
				{
					SoapHeader headerVal = hi.GetHeaderValue (target) as SoapHeader;
					if (headerVal != null)
						Headers.Add (headerVal);
				}
			}
		}
 // Methods
 public string ReadHeaders(System.Xml.XmlReader reader, System.Xml.Serialization.XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, string envelopeNS, string encodingStyle, bool checkRequiredHeaders)
 {
 }
 public static void SetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client)
 {
     bool[] flagArray = new bool[headers.Count];
     if (mappings != null)
     {
         for (int j = 0; j < mappings.Length; j++)
         {
             SoapHeaderMapping mapping = mappings[j];
             if ((mapping.direction & direction) != 0)
             {
                 if (mapping.repeats)
                 {
                     ArrayList list = new ArrayList();
                     for (int k = 0; k < headers.Count; k++)
                     {
                         SoapHeader header = headers[k];
                         if (!flagArray[k] && mapping.headerType.IsAssignableFrom(header.GetType()))
                         {
                             list.Add(header);
                             flagArray[k] = true;
                         }
                     }
                     MemberHelper.SetValue(mapping.memberInfo, target, list.ToArray(mapping.headerType));
                 }
                 else
                 {
                     bool flag = false;
                     for (int m = 0; m < headers.Count; m++)
                     {
                         SoapHeader header2 = headers[m];
                         if (!flagArray[m] && mapping.headerType.IsAssignableFrom(header2.GetType()))
                         {
                             if (flag)
                             {
                                 header2.DidUnderstand = false;
                             }
                             else
                             {
                                 flag = true;
                                 MemberHelper.SetValue(mapping.memberInfo, target, header2);
                                 flagArray[m] = true;
                             }
                         }
                     }
                 }
             }
         }
     }
     for (int i = 0; i < flagArray.Length; i++)
     {
         if (!flagArray[i])
         {
             SoapHeader header3 = headers[i];
             if (header3.MustUnderstand && !header3.DidUnderstand)
             {
                 throw new SoapHeaderException(System.Web.Services.Res.GetString("WebCannotUnderstandHeader", new object[] { GetHeaderElementName(header3) }), new XmlQualifiedName("MustUnderstand", "http://schemas.xmlsoap.org/soap/envelope/"));
             }
         }
     }
 }
Esempio n. 20
0
		public SoapHeaderAttribute (string memberName) 
		{
			direction = SoapHeaderDirection.In;
			this.memberName = memberName;
			required = true;
		}
Esempio n. 21
0
        // return first missing header name;
        public string ReadHeaders(XmlReader reader, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, string envelopeNS, string encodingStyle, bool checkRequiredHeaders) {
            string missingHeader = null;
            reader.MoveToContent();
            if (!reader.IsStartElement(Soap.Element.Header, envelopeNS)) {
                if (checkRequiredHeaders && mappings != null && mappings.Length > 0)
                    missingHeader = GetHeaderElementName(mappings[0].headerType);
                return missingHeader;
            }
            if (reader.IsEmptyElement) { reader.Skip(); return missingHeader; }

            this.unknownHeaders = new SoapHeaderCollection();
            this.unreferencedHeaders = new SoapHeaderCollection();
            // thread hash code is used to differentiate between deserializations in event callbacks
            this.currentThread = Thread.CurrentThread.GetHashCode();
            this.envelopeNS = envelopeNS;

            int depth = reader.Depth;
            reader.ReadStartElement();
            reader.MoveToContent();

            XmlDeserializationEvents events = new XmlDeserializationEvents();
            events.OnUnknownElement = new XmlElementEventHandler(this.OnUnknownElement);
            events.OnUnreferencedObject = new UnreferencedObjectEventHandler(this.OnUnreferencedObject);

            TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadHeaders") : null;
            if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceReadHeaders), caller, new TraceMethod(serializer, "Deserialize", reader, encodingStyle));
            object[] headerValues = (object[])serializer.Deserialize(reader, encodingStyle, events);
            if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceReadHeaders), caller);
            for (int i = 0; i < headerValues.Length; i++) {
                if (headerValues[i] != null) {
                    SoapHeader header = (SoapHeader)headerValues[i];
                    header.DidUnderstand = true;
                    headers.Add(header);
                }
                else if (checkRequiredHeaders) {
                    // run time check for R2738 A MESSAGE MUST include all soapbind:headers specified on a wsdl:input or wsdl:output of a wsdl:operationwsdl:binding that describes it. 
                    if (missingHeader == null)
                        missingHeader = GetHeaderElementName(mappings[i].headerType);
                }
            }
            this.currentThread = 0;
            this.envelopeNS = null;

            foreach (SoapHeader header in this.unreferencedHeaders) {
                headers.Add(header);
            }
            this.unreferencedHeaders = null;

            foreach (SoapHeader header in this.unknownHeaders) {
                headers.Add(header);
            }
            this.unknownHeaders = null;

            // Consume soap:Body and soap:Envelope closing tags
            while (depth < reader.Depth && reader.Read()) {
                // Nothing, just read on
            }
            // consume end tag
            if (reader.NodeType == XmlNodeType.EndElement) {
                reader.Read();
            }

            return missingHeader;
        }
Esempio n. 22
0
        public static void WriteHeaders(XmlWriter writer, XmlSerializer serializer, SoapHeaderCollection headers, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool isEncoded, string defaultNS, bool serviceDefaultIsEncoded, string envelopeNS) {
            if (headers.Count == 0) return;
            writer.WriteStartElement(Soap.Element.Header, envelopeNS);
            SoapProtocolVersion version;
            string encodingStyle;
            if (envelopeNS == Soap12.Namespace) {
                version = SoapProtocolVersion.Soap12;
                encodingStyle = Soap12.Encoding;
            }
            else {
                version = SoapProtocolVersion.Soap11;
                encodingStyle = Soap.Encoding;
            }

            int unknownHeaderCount = 0;
            ArrayList otherHeaders = new ArrayList();
            SoapHeader[] headerArray = new SoapHeader[mappings.Length];
            bool[] headerSet = new bool[headerArray.Length];
            for (int i = 0; i < headers.Count; i++) {
                SoapHeader header = headers[i];
                if (header == null) continue;
                int headerPosition;
                header.version = version;
                if (header is SoapUnknownHeader) {
                    otherHeaders.Add(header);
                    unknownHeaderCount++;
                }
                else if ((headerPosition = FindMapping(mappings, header, direction)) >= 0 && !headerSet[headerPosition]) {
                    headerArray[headerPosition] = header;
                    headerSet[headerPosition] = true;
                }
                else {
                    otherHeaders.Add(header);
                }
            }
            int otherHeaderCount = otherHeaders.Count - unknownHeaderCount;
            if (isEncoded && otherHeaderCount > 0) {
                SoapHeader[] newHeaderArray = new SoapHeader[mappings.Length + otherHeaderCount];
                headerArray.CopyTo(newHeaderArray, 0);

                // fill in the non-statically known headers (otherHeaders) starting after the statically-known ones
                int count = mappings.Length;
                for (int i = 0; i < otherHeaders.Count; i++) {
                    if (!(otherHeaders[i] is SoapUnknownHeader))
                        newHeaderArray[count++] = (SoapHeader)otherHeaders[i];
                }

                headerArray = newHeaderArray;
            }

            TraceMethod caller = Tracing.On ? new TraceMethod(typeof(SoapHeaderHandling), "WriteHeaders") : null;
            if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(serializer, "Serialize", writer, headerArray, null, isEncoded ? encodingStyle : null, "h_"));
            serializer.Serialize(writer, headerArray, null, isEncoded ? encodingStyle : null, "h_");
            if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller);

            foreach (SoapHeader header in otherHeaders) {
                if (header is SoapUnknownHeader) {
                    SoapUnknownHeader unknown = (SoapUnknownHeader)header;
                    if (unknown.Element != null)
                        unknown.Element.WriteTo(writer);
                }
                else if (!isEncoded) { // encoded headers already appended to members mapping
                    string ns = SoapReflector.GetLiteralNamespace(defaultNS, serviceDefaultIsEncoded);
                    XmlSerializer headerSerializer = new XmlSerializer(header.GetType(), ns);

                    if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceWriteHeaders), caller, new TraceMethod(headerSerializer, "Serialize", writer, header));
                    headerSerializer.Serialize(writer, header);
                    if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceWriteHeaders), caller);
                }
            }

            // reset the soap version
            for (int i = 0; i < headers.Count; i++) {
                SoapHeader header = headers[i];
                if (header != null)
                    header.version = SoapProtocolVersion.Default;
            }

            writer.WriteEndElement();
            writer.Flush();
        }
Esempio n. 23
0
		public XmlSerializer GetBodySerializer (SoapHeaderDirection dir, bool soap12)
		{
			switch (dir) {
				case SoapHeaderDirection.In: return RequestSerializer;
				case SoapHeaderDirection.Out: return ResponseSerializer;
				case SoapHeaderDirection.Fault: return soap12 ? Soap12Fault.Serializer : Fault.Serializer;
				default: return null;
			}
		}
Esempio n. 24
0
 public static void SetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client) {
     bool[] headerHandled = new bool[headers.Count];
     if (mappings != null) {
         for (int i = 0; i < mappings.Length; i++) {
             SoapHeaderMapping mapping = mappings[i];
             if ((mapping.direction & direction) == 0) continue;
             if (mapping.repeats) {
                 ArrayList list = new ArrayList();
                 for (int j = 0; j < headers.Count; j++) {
                     SoapHeader header = headers[j];
                     if (headerHandled[j]) continue;
                     if (mapping.headerType.IsAssignableFrom(header.GetType())) {
                         list.Add(header);
                         headerHandled[j] = true;
                     }
                 }
                 MemberHelper.SetValue(mapping.memberInfo, target, list.ToArray(mapping.headerType));
             }
             else {
                 bool handled = false;
                 for (int j = 0; j < headers.Count; j++) {
                     SoapHeader header = headers[j];
                     if (headerHandled[j]) continue;
                     if (mapping.headerType.IsAssignableFrom(header.GetType())) {
                         if (handled) {
                             header.DidUnderstand = false;
                             continue;
                         }
                         handled = true;
                         MemberHelper.SetValue(mapping.memberInfo, target, header);
                         headerHandled[j] = true;
                     }
                 }
             }
         }
     }
     for (int i = 0; i < headerHandled.Length; i++) {
         if (!headerHandled[i]) {
             SoapHeader header = headers[i];
             if (header.MustUnderstand && !header.DidUnderstand) {
                 throw new SoapHeaderException(Res.GetString(Res.WebCannotUnderstandHeader, GetHeaderElementName(header)),
                     new XmlQualifiedName(Soap.Code.MustUnderstand, Soap.Namespace));
             }
         }
     }
 }
Esempio n. 25
0
		HeaderInfo[] GetHeaders (SoapHeaderDirection dir)
		{
			switch (dir) {
				case SoapHeaderDirection.In: return InHeaders;
				case SoapHeaderDirection.Out: return OutHeaders;
				case SoapHeaderDirection.Fault: return FaultHeaders;
				default: return null;
			}
		}
Esempio n. 26
0
 public static void GetHeaderMembers(SoapHeaderCollection headers, object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, bool client) {
     if (mappings == null || mappings.Length == 0) return;
     for (int i = 0; i < mappings.Length; i++) {
         SoapHeaderMapping mapping = mappings[i];
         if ((mapping.direction & direction) == 0) continue;
         object value = MemberHelper.GetValue(mapping.memberInfo, target);
         if (mapping.repeats) {
             object[] values = (object[])value;
             if (values == null) continue;
             for (int j = 0; j < values.Length; j++) {
                 if (values[j] != null) headers.Add((SoapHeader)values[j]);
             }
         }
         else {
             if (value != null) headers.Add((SoapHeader)value);
         }
     }
 }
		void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
		{
			Message msg = ServiceDescriptions.GetMessage (hb.Message);
			if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
			MessagePart part = msg.Parts [hb.Part];
			if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);

			XmlTypeMapping map;
			if (hb.Use == SoapBindingUse.Literal)
			{
				map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
				xmlExporter.ExportTypeMapping (map);
			}
			else
			{
				map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
				soapExporter.ExportTypeMapping (map);
			}

			bool required = false;

			string varName = headerVariables [map] as string;
			if (varName == null) 
			{
				varName = memberIds.AddUnique(CodeIdentifier.MakeValid (map.TypeName + "Value"),hb);
				headerVariables.Add (map, varName);
				CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
				codeField.Attributes = MemberAttributes.Public;
				CodeTypeDeclaration.Members.Add (codeField);
			}
			
			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
			att.Arguments.Add (GetArg (varName));
			att.Arguments.Add (GetArg ("Required", required));
			if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
			AddCustomAttribute (method, att, true);
		}
        internal static SoapReflectedMethod ReflectMethod(LogicalMethodInfo methodInfo, bool client, XmlReflectionImporter xmlImporter, SoapReflectionImporter soapImporter, string defaultNs)
        {
            try {
                string methodId = methodInfo.GetKey();
                SoapReflectedMethod soapMethod      = new SoapReflectedMethod();
                MethodAttribute     methodAttribute = new MethodAttribute();

                object serviceAttr             = GetSoapServiceAttribute(methodInfo.DeclaringType);
                bool   serviceDefaultIsEncoded = ServiceDefaultIsEncoded(serviceAttr);
                object methodAttr = GetSoapMethodAttribute(methodInfo);
                if (methodAttr == null)
                {
                    if (client)
                    {
                        return(null);        // method attribute required on the client
                    }
                    if (serviceAttr is SoapRpcServiceAttribute)
                    {
                        SoapRpcMethodAttribute method = new SoapRpcMethodAttribute();
                        method.Use = ((SoapRpcServiceAttribute)serviceAttr).Use;
                        methodAttr = method;
                    }
                    else if (serviceAttr is SoapDocumentServiceAttribute)
                    {
                        SoapDocumentMethodAttribute method = new SoapDocumentMethodAttribute();
                        method.Use = ((SoapDocumentServiceAttribute)serviceAttr).Use;
                        methodAttr = method;
                    }
                    else
                    {
                        methodAttr = new SoapDocumentMethodAttribute();
                    }
                }

                if (methodAttr is SoapRpcMethodAttribute)
                {
                    SoapRpcMethodAttribute attr = (SoapRpcMethodAttribute)methodAttr;

                    soapMethod.rpc               = true;
                    soapMethod.use               = attr.Use;
                    soapMethod.oneWay            = attr.OneWay;
                    methodAttribute.action       = attr.Action;
                    methodAttribute.binding      = attr.Binding;
                    methodAttribute.requestName  = attr.RequestElementName;
                    methodAttribute.requestNs    = attr.RequestNamespace;
                    methodAttribute.responseName = attr.ResponseElementName;
                    methodAttribute.responseNs   = attr.ResponseNamespace;
                }
                else
                {
                    SoapDocumentMethodAttribute attr = (SoapDocumentMethodAttribute)methodAttr;

                    soapMethod.rpc               = false;
                    soapMethod.use               = attr.Use;
                    soapMethod.paramStyle        = attr.ParameterStyle;
                    soapMethod.oneWay            = attr.OneWay;
                    methodAttribute.action       = attr.Action;
                    methodAttribute.binding      = attr.Binding;
                    methodAttribute.requestName  = attr.RequestElementName;
                    methodAttribute.requestNs    = attr.RequestNamespace;
                    methodAttribute.responseName = attr.ResponseElementName;
                    methodAttribute.responseNs   = attr.ResponseNamespace;

                    if (soapMethod.use == SoapBindingUse.Default)
                    {
                        if (serviceAttr is SoapDocumentServiceAttribute)
                        {
                            soapMethod.use = ((SoapDocumentServiceAttribute)serviceAttr).Use;
                        }
                        if (soapMethod.use == SoapBindingUse.Default)
                        {
                            soapMethod.use = SoapBindingUse.Literal;
                        }
                    }
                    if (soapMethod.paramStyle == SoapParameterStyle.Default)
                    {
                        if (serviceAttr is SoapDocumentServiceAttribute)
                        {
                            soapMethod.paramStyle = ((SoapDocumentServiceAttribute)serviceAttr).ParameterStyle;
                        }
                        if (soapMethod.paramStyle == SoapParameterStyle.Default)
                        {
                            soapMethod.paramStyle = SoapParameterStyle.Wrapped;
                        }
                    }
                }

                if (methodAttribute.binding.Length > 0)
                {
                    if (client)
                    {
                        throw new InvalidOperationException(Res.GetString(Res.WebInvalidBindingPlacement, methodAttr.GetType().Name));
                    }
                    soapMethod.binding = WebServiceBindingReflector.GetAttribute(methodInfo, methodAttribute.binding);
                }

                WebMethodAttribute webMethodAttribute = methodInfo.MethodAttribute;

                //
                soapMethod.name = webMethodAttribute.MessageName;
                if (soapMethod.name.Length == 0)
                {
                    soapMethod.name = methodInfo.Name;
                }

                string requestElementName;
                if (soapMethod.rpc)
                {
                    // in the case when we interop with non .net we might need to chnage the method name.
                    requestElementName = methodAttribute.requestName.Length == 0 || !client ? methodInfo.Name : methodAttribute.requestName;
                }
                else
                {
                    requestElementName = methodAttribute.requestName.Length == 0 ? soapMethod.name : methodAttribute.requestName;
                }
                string requestNamespace = methodAttribute.requestNs;

                if (requestNamespace == null)
                {
                    if (soapMethod.binding != null && soapMethod.binding.Namespace != null && soapMethod.binding.Namespace.Length != 0)
                    {
                        requestNamespace = soapMethod.binding.Namespace;
                    }
                    else
                    {
                        requestNamespace = defaultNs;
                    }
                }

                string responseElementName;
                if (soapMethod.rpc && soapMethod.use != SoapBindingUse.Encoded)
                {
                    // NOTE: this rule should apply equally to rpc/lit and rpc/enc, but to reduce risk, i'm only applying it to rpc/lit
                    responseElementName = methodInfo.Name + "Response";
                }
                else
                {
                    responseElementName = methodAttribute.responseName.Length == 0 ? soapMethod.name + "Response" : methodAttribute.responseName;
                }
                string responseNamespace = methodAttribute.responseNs;

                if (responseNamespace == null)
                {
                    if (soapMethod.binding != null && soapMethod.binding.Namespace != null && soapMethod.binding.Namespace.Length != 0)
                    {
                        responseNamespace = soapMethod.binding.Namespace;
                    }
                    else
                    {
                        responseNamespace = defaultNs;
                    }
                }

                SoapParameterInfo[] inParameters  = ReflectParameters(methodInfo.InParameters, requestNamespace);
                SoapParameterInfo[] outParameters = ReflectParameters(methodInfo.OutParameters, responseNamespace);

                soapMethod.action = methodAttribute.action;
                if (soapMethod.action == null)
                {
                    soapMethod.action = GetDefaultAction(defaultNs, methodInfo);
                }
                soapMethod.methodInfo = methodInfo;

                if (soapMethod.oneWay)
                {
                    if (outParameters.Length > 0)
                    {
                        throw new ArgumentException(Res.GetString(Res.WebOneWayOutParameters), "methodInfo");
                    }
                    if (methodInfo.ReturnType != typeof(void))
                    {
                        throw new ArgumentException(Res.GetString(Res.WebOneWayReturnValue), "methodInfo");
                    }
                }

                XmlReflectionMember[] members = new XmlReflectionMember[inParameters.Length];
                for (int i = 0; i < members.Length; i++)
                {
                    SoapParameterInfo   soapParamInfo = inParameters[i];
                    XmlReflectionMember member        = new XmlReflectionMember();
                    member.MemberName = soapParamInfo.parameterInfo.Name;
                    member.MemberType = soapParamInfo.parameterInfo.ParameterType;
                    if (member.MemberType.IsByRef)
                    {
                        member.MemberType = member.MemberType.GetElementType();
                    }
                    member.XmlAttributes  = soapParamInfo.xmlAttributes;
                    member.SoapAttributes = soapParamInfo.soapAttributes;
                    members[i]            = member;
                }
                soapMethod.requestMappings = ImportMembersMapping(xmlImporter, soapImporter, serviceDefaultIsEncoded, soapMethod.rpc, soapMethod.use, soapMethod.paramStyle, requestElementName, requestNamespace, methodAttribute.requestNs == null, members, true, false, methodId, client);

                if (GetSoapServiceRoutingStyle(serviceAttr) == SoapServiceRoutingStyle.RequestElement &&
                    soapMethod.paramStyle == SoapParameterStyle.Bare &&
                    soapMethod.requestMappings.Count != 1)
                {
                    throw new ArgumentException(Res.GetString(Res.WhenUsingAMessageStyleOfParametersAsDocument0), "methodInfo");
                }

                string elementName      = "";
                string elementNamespace = "";
                if (soapMethod.paramStyle == SoapParameterStyle.Bare)
                {
                    if (soapMethod.requestMappings.Count == 1)
                    {
                        elementName      = soapMethod.requestMappings[0].XsdElementName;
                        elementNamespace = soapMethod.requestMappings[0].Namespace;
                    }
                    // else: can't route on request element -- we match on an empty qname,
                    //       normal rules apply for duplicates
                }
                else
                {
                    elementName      = soapMethod.requestMappings.XsdElementName;
                    elementNamespace = soapMethod.requestMappings.Namespace;
                }
                soapMethod.requestElementName = new XmlQualifiedName(elementName, elementNamespace);

                if (!soapMethod.oneWay)
                {
                    int             numOutParams = outParameters.Length;
                    int             count        = 0;
                    CodeIdentifiers identifiers  = null;
                    if (methodInfo.ReturnType != typeof(void))
                    {
                        numOutParams++;
                        count       = 1;
                        identifiers = new CodeIdentifiers();
                    }
                    members = new XmlReflectionMember[numOutParams];

                    for (int i = 0; i < outParameters.Length; i++)
                    {
                        SoapParameterInfo   soapParamInfo = outParameters[i];
                        XmlReflectionMember member        = new XmlReflectionMember();
                        member.MemberName = soapParamInfo.parameterInfo.Name;
                        member.MemberType = soapParamInfo.parameterInfo.ParameterType;
                        if (member.MemberType.IsByRef)
                        {
                            member.MemberType = member.MemberType.GetElementType();
                        }
                        member.XmlAttributes  = soapParamInfo.xmlAttributes;
                        member.SoapAttributes = soapParamInfo.soapAttributes;
                        members[count++]      = member;
                        if (identifiers != null)
                        {
                            identifiers.Add(member.MemberName, null);
                        }
                    }
                    if (methodInfo.ReturnType != typeof(void))
                    {
                        XmlReflectionMember member = new XmlReflectionMember();
                        member.MemberName    = identifiers.MakeUnique(soapMethod.name + "Result");
                        member.MemberType    = methodInfo.ReturnType;
                        member.IsReturnValue = true;

                        member.XmlAttributes         = new XmlAttributes(methodInfo.ReturnTypeCustomAttributeProvider);
                        member.XmlAttributes.XmlRoot = null; // Ignore XmlRoot attribute used by get/post
                        member.SoapAttributes        = new SoapAttributes(methodInfo.ReturnTypeCustomAttributeProvider);

                        members[0] = member;
                    }
                    soapMethod.responseMappings = ImportMembersMapping(xmlImporter, soapImporter, serviceDefaultIsEncoded, soapMethod.rpc, soapMethod.use, soapMethod.paramStyle, responseElementName, responseNamespace, methodAttribute.responseNs == null, members, false, false, methodId + ":Response", !client);
                }

                SoapExtensionAttribute[] extensionAttributes = (SoapExtensionAttribute[])methodInfo.GetCustomAttributes(typeof(SoapExtensionAttribute));
                soapMethod.extensions = new SoapReflectedExtension[extensionAttributes.Length];
                for (int i = 0; i < extensionAttributes.Length; i++)
                {
                    soapMethod.extensions[i] = new SoapReflectedExtension(extensionAttributes[i].ExtensionType, extensionAttributes[i]);
                }
                Array.Sort(soapMethod.extensions);

                SoapHeaderAttribute[] headerAttributes = (SoapHeaderAttribute[])methodInfo.GetCustomAttributes(typeof(SoapHeaderAttribute));
                Array.Sort(headerAttributes, new SoapHeaderAttributeComparer());
                Hashtable headerTypes = new Hashtable();
                soapMethod.headers = new SoapReflectedHeader[headerAttributes.Length];
                int       front      = 0;
                int       back       = soapMethod.headers.Length;
                ArrayList inHeaders  = new ArrayList();
                ArrayList outHeaders = new ArrayList();
                for (int i = 0; i < soapMethod.headers.Length; i++)
                {
                    SoapHeaderAttribute headerAttribute = headerAttributes[i];
                    SoapReflectedHeader soapHeader      = new SoapReflectedHeader();
                    Type declaringType = methodInfo.DeclaringType;
                    if ((soapHeader.memberInfo = declaringType.GetField(headerAttribute.MemberName)) != null)
                    {
                        soapHeader.headerType = ((FieldInfo)soapHeader.memberInfo).FieldType;
                    }
                    else if ((soapHeader.memberInfo = declaringType.GetProperty(headerAttribute.MemberName)) != null)
                    {
                        soapHeader.headerType = ((PropertyInfo)soapHeader.memberInfo).PropertyType;
                    }
                    else
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderMissing);
                    }
                    if (soapHeader.headerType.IsArray)
                    {
                        soapHeader.headerType = soapHeader.headerType.GetElementType();
                        soapHeader.repeats    = true;
                        if (soapHeader.headerType != typeof(SoapUnknownHeader) && soapHeader.headerType != typeof(SoapHeader))
                        {
                            throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderType);
                        }
                    }
                    if (MemberHelper.IsStatic(soapHeader.memberInfo))
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderStatic);
                    }
                    if (!MemberHelper.CanRead(soapHeader.memberInfo))
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderRead);
                    }
                    if (!MemberHelper.CanWrite(soapHeader.memberInfo))
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderWrite);
                    }
                    if (!typeof(SoapHeader).IsAssignableFrom(soapHeader.headerType))
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderType);
                    }

                    SoapHeaderDirection direction = headerAttribute.Direction;
                    if (soapMethod.oneWay && (direction & (SoapHeaderDirection.Out | SoapHeaderDirection.Fault)) != 0)
                    {
                        throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebHeaderOneWayOut);
                    }
                    if (headerTypes.Contains(soapHeader.headerType))
                    {
                        SoapHeaderDirection prevDirection = (SoapHeaderDirection)headerTypes[soapHeader.headerType];
                        if ((prevDirection & direction) != 0)
                        {
                            throw HeaderException(headerAttribute.MemberName, methodInfo.DeclaringType, Res.WebMultiplyDeclaredHeaderTypes);
                        }
                        headerTypes[soapHeader.headerType] = direction | prevDirection;
                    }
                    else
                    {
                        headerTypes[soapHeader.headerType] = direction;
                    }

                    if (soapHeader.headerType != typeof(SoapHeader) && soapHeader.headerType != typeof(SoapUnknownHeader))
                    {
                        XmlReflectionMember member = new XmlReflectionMember();
                        member.MemberName = soapHeader.headerType.Name;
                        member.MemberType = soapHeader.headerType;

                        XmlAttributes a = new XmlAttributes(soapHeader.headerType);
                        if (a.XmlRoot != null)
                        {
                            member.XmlAttributes = new XmlAttributes();
                            XmlElementAttribute attr = new XmlElementAttribute();
                            attr.ElementName = a.XmlRoot.ElementName;
                            attr.Namespace   = a.XmlRoot.Namespace;
                            member.XmlAttributes.XmlElements.Add(attr);
                        }
                        member.OverrideIsNullable = true;

                        if ((direction & SoapHeaderDirection.In) != 0)
                        {
                            inHeaders.Add(member);
                        }
                        if ((direction & (SoapHeaderDirection.Out | SoapHeaderDirection.Fault)) != 0)
                        {
                            outHeaders.Add(member);
                        }

                        soapHeader.custom = true;
                    }
                    soapHeader.direction = direction;
                    // Put generic header mappings at the end of the list so they are found last during header processing
                    if (!soapHeader.custom)
                    {
                        soapMethod.headers[--back] = soapHeader;
                    }
                    else
                    {
                        soapMethod.headers[front++] = soapHeader;
                    }
                }
                soapMethod.inHeaderMappings = ImportMembersMapping(xmlImporter, soapImporter, serviceDefaultIsEncoded, false, soapMethod.use, SoapParameterStyle.Bare, requestElementName + "InHeaders", defaultNs, true, (XmlReflectionMember[])inHeaders.ToArray(typeof(XmlReflectionMember)), false, true, methodId + ":InHeaders", client);
                if (!soapMethod.oneWay)
                {
                    soapMethod.outHeaderMappings = ImportMembersMapping(xmlImporter, soapImporter, serviceDefaultIsEncoded, false, soapMethod.use, SoapParameterStyle.Bare, responseElementName + "OutHeaders", defaultNs, true, (XmlReflectionMember[])outHeaders.ToArray(typeof(XmlReflectionMember)), false, true, methodId + ":OutHeaders", !client);
                }

                return(soapMethod);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException)
                {
                    throw;
                }
                throw new InvalidOperationException(Res.GetString(Res.WebReflectionErrorMethod, methodInfo.DeclaringType.Name, methodInfo.Name), e);
            }
        }