Fix to UriTemplate.Match to properly handle query parameters without a value. No...
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / SoapProtocolImporter.cs
1 // 
2 // System.Web.Services.Description.SoapProtocolImporter.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.CodeDom;
33 using System.Web.Services;
34 using System.Web.Services.Protocols;
35 using System.Web.Services.Configuration;
36 using System.Xml;
37 using System.Xml.Schema;
38 using System.Xml.Serialization;
39 using System.Configuration;
40 using System.Collections;
41
42 namespace System.Web.Services.Description {
43         public class SoapProtocolImporter : ProtocolImporter {
44
45                 #region Fields
46
47                 SoapBinding soapBinding;
48                 SoapCodeExporter soapExporter;
49                 SoapSchemaImporter soapImporter;
50                 XmlCodeExporter xmlExporter;
51                 XmlSchemaImporter xmlImporter;
52                 CodeIdentifiers memberIds;
53                 ArrayList extensionImporters;
54                 Hashtable headerVariables;
55                 XmlSchemas xmlSchemas;
56                 XmlSchemas soapSchemas;
57                 
58                 #endregion // Fields
59
60                 #region Constructors
61
62                 public SoapProtocolImporter ()
63                 {
64                         extensionImporters = ExtensionManager.BuildExtensionImporters ();
65                 }
66                 
67                 void SetBinding (SoapBinding soapBinding)
68                 {
69                         this.soapBinding = soapBinding;
70                 }
71                 
72                 #endregion // Constructors
73
74                 #region Properties
75
76                 public override string ProtocolName {
77                         get { return "Soap"; }
78                 }
79
80                 public SoapBinding SoapBinding {
81                         get { return soapBinding; }
82                 }
83
84                 public SoapCodeExporter SoapExporter {
85                         get { return soapExporter; }
86                 }
87
88                 public SoapSchemaImporter SoapImporter {
89                         get { return soapImporter; }
90                 }
91
92                 public XmlCodeExporter XmlExporter {
93                         get { return xmlExporter; }
94                 }
95
96                 public XmlSchemaImporter XmlImporter {
97                         get { return xmlImporter; }
98                 }
99
100                 #endregion // Properties
101
102                 #region Methods
103
104                 protected override CodeTypeDeclaration BeginClass ()
105                 {
106                         soapBinding = (SoapBinding) Binding.Extensions.Find (typeof(SoapBinding));
107                         
108                         CodeTypeDeclaration codeClass = new CodeTypeDeclaration (ClassName);
109                         codeClass.IsPartial = true;
110
111                         string location = null;
112                         
113                         if (Port != null) {
114                                 SoapAddressBinding sab = (SoapAddressBinding) Port.Extensions.Find (typeof(SoapAddressBinding));
115                                 if (sab != null) location = sab.Location;
116                         }
117                         
118                         string namspace = (Port != null ? Port.Binding.Namespace : Binding.ServiceDescription.TargetNamespace);
119                         string name = (Port != null ? Port.Name : Binding.Name);
120
121                         if (Style == ServiceDescriptionImportStyle.Client) {
122                                 CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.Protocols.SoapHttpClientProtocol");
123                                 codeClass.BaseTypes.Add (ctr);
124                         }
125                         else {
126                                 CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.WebService");
127                                 codeClass.BaseTypes.Add (ctr);
128                                 CodeAttributeDeclaration attws = new CodeAttributeDeclaration ("System.Web.Services.WebServiceAttribute");
129                                 attws.Arguments.Add (GetArg ("Namespace", namspace));
130                                 AddCustomAttribute (codeClass, attws, true);
131                         }
132                         
133                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebServiceBinding");
134                         att.Arguments.Add (GetArg ("Name", name));
135                         att.Arguments.Add (GetArg ("Namespace", namspace));
136                         AddCustomAttribute (codeClass, att, true);
137         
138                         if (Style == ServiceDescriptionImportStyle.Client) {
139                                 CodeConstructor cc = new CodeConstructor ();
140                                 cc.Attributes = MemberAttributes.Public;
141                                 GenerateServiceUrl (location, cc.Statements);
142
143                                 if (ProtocolName.ToUpper () == "SOAP12") {
144                                         CodeExpression thisSoapVer = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "SoapVersion");
145                                         CodeFieldReferenceExpression soap12Enum =
146                                                 new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeof (SoapProtocolVersion)), "Soap12");
147                                         cc.Statements.Add (new CodeAssignStatement (thisSoapVer, soap12Enum));
148                                 }
149                                 codeClass.Members.Add (cc);
150                         }
151                         
152                         memberIds = new CodeIdentifiers ();
153                         headerVariables = new Hashtable ();
154                         return codeClass;
155                 }
156                 
157                 protected override void BeginNamespace ()
158                 {
159                         xmlImporter = new XmlSchemaImporter (LiteralSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
160                         soapImporter = new SoapSchemaImporter (EncodedSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
161                         xmlExporter = new XmlCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
162                         soapExporter = new SoapCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
163                 }
164
165                 protected override void EndClass ()
166                 {
167                         SoapTransportImporter transportImporter = SoapTransportImporter.FindTransportImporter (soapBinding.Transport);
168                         if (transportImporter == null) throw new InvalidOperationException ("Transport '" + soapBinding.Transport + "' not supported");
169                         transportImporter.ImportContext = this;
170                         transportImporter.ImportClass ();
171                         
172                         if (xmlExporter.IncludeMetadata.Count > 0 || soapExporter.IncludeMetadata.Count > 0)
173                         {
174                                 if (CodeTypeDeclaration.CustomAttributes == null)
175                                         CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
176                                 CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
177                                 CodeTypeDeclaration.CustomAttributes.AddRange (soapExporter.IncludeMetadata);
178                         }
179                 }
180
181                 protected override void EndNamespace ()
182                 {
183                 }
184
185                 protected override bool IsBindingSupported ()
186                 {
187                         object o = Binding.Extensions.Find (typeof(SoapBinding));
188                         return o != null && !(o is Soap12Binding);
189                 }
190
191                 [MonoTODO]
192                 protected override bool IsOperationFlowSupported (OperationFlow flow)
193                 {
194                         throw new NotImplementedException ();
195                 }
196
197                 static readonly char [] whitespaces = new char [] {' ', '\t', '\n', '\r'};
198
199                 protected virtual bool IsSoapEncodingPresent (string uriList)
200                 {
201                         foreach (string s in uriList.Split (whitespaces))
202                                 if (s == "http://schemas.xmlsoap.org/soap/encoding/")
203                                         return true;
204                         return false;
205                 }
206
207                 protected override CodeMemberMethod GenerateMethod ()
208                 {
209                         try
210                         {
211                                 SoapOperationBinding soapOper = OperationBinding.Extensions.Find (typeof (SoapOperationBinding)) as SoapOperationBinding;
212                                 if (soapOper == null) throw new InvalidOperationException ("Soap operation binding not found");
213
214                                 SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
215                         
216                                 SoapBodyBinding isbb = null;
217                                 XmlMembersMapping inputMembers = null;
218                                 
219                                 bool isWrapped = CheckIsWrapped ();
220                                 
221                                 isbb = OperationBinding.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
222                                 if (isbb == null) throw new InvalidOperationException ("Soap body binding not found");
223                         
224                                 inputMembers = ImportMembersMapping (InputMessage, isbb, style, false, isWrapped);
225                                 if (inputMembers == null) throw new InvalidOperationException ("Input message not declared");
226                                 
227                                 // If OperationBinding.Output is null, it is an OneWay operation
228                                 
229                                 SoapBodyBinding osbb = null;
230                                 XmlMembersMapping outputMembers = null;
231                                 
232                                 if (OperationBinding.Output != null) {
233                                         osbb = OperationBinding.Output.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
234                                         if (osbb == null) throw new InvalidOperationException ("Soap body binding not found");
235
236                                         outputMembers = ImportMembersMapping (OutputMessage, osbb, style, true, isWrapped);
237                                         if (outputMembers == null) throw new InvalidOperationException ("Output message not declared");
238                                 }
239                                 
240                                 CodeMemberMethod met = GenerateMethod (memberIds, soapOper, isbb, inputMembers, outputMembers);
241                                 
242                                 if (isbb.Use == SoapBindingUse.Literal)
243                                         xmlExporter.ExportMembersMapping (inputMembers);
244                                 else
245                                         soapExporter.ExportMembersMapping (inputMembers);
246                                 
247                                 if (osbb != null) {
248                                         if (osbb.Use == SoapBindingUse.Literal)
249                                                 xmlExporter.ExportMembersMapping (outputMembers);
250                                         else
251                                                 soapExporter.ExportMembersMapping (outputMembers);
252                                 }
253                                 
254                                 foreach (SoapExtensionImporter eximporter in extensionImporters)
255                                 {
256                                         eximporter.ImportContext = this;
257                                         eximporter.ImportMethod (met.CustomAttributes);
258                                 }
259                                 
260                                 return met;
261                         }
262                         catch (InvalidOperationException ex)
263                         {
264                                 UnsupportedOperationBindingWarning (ex.Message);
265                                 return null;
266                         }
267                 }
268                 
269                 bool CheckIsWrapped ()
270                 {
271                         return (OutputMessage == null || (OutputMessage.Parts.Count == 1 && OutputMessage.Parts[0].Name == "parameters")) &&
272                                    (InputMessage == null || (InputMessage.Parts.Count == 1 && InputMessage.Parts[0].Name == "parameters"));
273                 }
274                 
275                 XmlMembersMapping ImportMembersMapping (Message msg, SoapBodyBinding sbb, SoapBindingStyle style, bool output, bool wrapped)
276                 {
277                         string elemName = Operation.Name;
278                         if (output) elemName += "Response";
279
280                         if (wrapped)
281                         {
282                                 // Wrapped parameter style
283                                 
284                                 MessagePart part = msg.Parts[0];
285                                 if (sbb.Use == SoapBindingUse.Encoded)
286                                 {
287                                         SoapSchemaMember ssm = new SoapSchemaMember ();
288                                         ssm.MemberName = part.Name;
289                                         ssm.MemberType = part.Type;
290                                         return soapImporter.ImportMembersMapping (elemName, part.Type.Namespace, ssm);
291                                 }
292                                 else
293                                         return xmlImporter.ImportMembersMapping (part.Element);
294                         }
295                         else
296                         {
297                                 if (sbb.Use == SoapBindingUse.Encoded)
298                                 {
299                                         SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
300                                         for (int n=0; n<mems.Length; n++)
301                                         {
302                                                 SoapSchemaMember mem = new SoapSchemaMember();
303                                                 mem.MemberName = msg.Parts[n].Name;
304                                                 mem.MemberType = msg.Parts[n].Type;
305                                                 mems[n] = mem;
306                                         }
307                                         
308                                         // Rpc messages always have a wrapping element
309                                         if (style == SoapBindingStyle.Rpc)
310                                                 return soapImporter.ImportMembersMapping (elemName, sbb.Namespace, mems, true);
311                                         else
312                                                 return soapImporter.ImportMembersMapping ("", "", mems, false);
313                                 }
314                                 else
315                                 {
316                                         if (style == SoapBindingStyle.Rpc)
317                                                 throw new InvalidOperationException ("The combination of style=rpc with use=literal is not supported");
318                                         
319                                         if (msg.Parts.Count == 1 && msg.Parts[0].Type != XmlQualifiedName.Empty)
320                                                 return xmlImporter.ImportAnyType (msg.Parts[0].Type, null);
321                                         else
322                                         {
323                                                 XmlQualifiedName[] pnames = new XmlQualifiedName [msg.Parts.Count];
324                                                 for (int n=0; n<pnames.Length; n++)
325                                                         pnames[n] = msg.Parts[n].Element;
326                                                 return xmlImporter.ImportMembersMapping (pnames);
327                                         }
328                                 }
329                         }
330                 }
331                 
332                 CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, SoapOperationBinding soapOper, SoapBodyBinding bodyBinding, XmlMembersMapping inputMembers, XmlMembersMapping outputMembers)
333                 {
334                         CodeIdentifiers pids = new CodeIdentifiers ();
335                         CodeMemberMethod method = new CodeMemberMethod ();
336                         CodeMemberMethod methodBegin = new CodeMemberMethod ();
337                         CodeMemberMethod methodEnd = new CodeMemberMethod ();
338                         method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
339                         methodBegin.Attributes = MemberAttributes.Public | MemberAttributes.Final;
340                         methodEnd.Attributes = MemberAttributes.Public | MemberAttributes.Final;
341                         
342                         SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
343                         
344                         // Find unique names for temporary variables
345                         
346                         for (int n=0; n<inputMembers.Count; n++)
347                                 pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
348
349                         if (outputMembers != null)
350                                 for (int n=0; n<outputMembers.Count; n++)
351                                         pids.AddUnique (outputMembers[n].MemberName, outputMembers[n]);
352                                 
353                         string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
354                         string varResults = pids.AddUnique ("results","results");
355                         string varCallback = pids.AddUnique ("callback","callback");
356                         string varAsyncState = pids.AddUnique ("asyncState","asyncState");
357
358                         string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
359
360                         method.Name = CodeIdentifier.MakeValid(Operation.Name);
361                         if (method.Name == ClassName) method.Name += "1";
362                         methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + method.Name),method);
363                         methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + method.Name),method);
364
365                         method.ReturnType = new CodeTypeReference (typeof(void));
366                         methodEnd.ReturnType = new CodeTypeReference (typeof(void));
367                         methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
368
369                         CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
370                         CodeParameterDeclarationExpression[] outParams = new CodeParameterDeclarationExpression [outputMembers != null ? outputMembers.Count : 0];
371
372                         for (int n=0; n<inputMembers.Count; n++)
373                         {
374                                 CodeParameterDeclarationExpression param = GenerateParameter (inputMembers[n], FieldDirection.In);
375                                 method.Parameters.Add (param);
376                                 GenerateMemberAttributes (inputMembers, inputMembers[n], bodyBinding.Use, param);
377                                 methodBegin.Parameters.Add (GenerateParameter (inputMembers[n], FieldDirection.In));
378                                 paramArray [n] = new CodeVariableReferenceExpression (param.Name);
379                         }
380
381                         if (outputMembers != null)
382                         {
383                                 bool hasReturn = false;
384                                 for (int n=0; n<outputMembers.Count; n++)
385                                 {
386                                         CodeParameterDeclarationExpression cpd = GenerateParameter (outputMembers[n], FieldDirection.Out);
387                                         outParams [n] = cpd;
388                                         
389                                         bool found = false;
390                                         foreach (CodeParameterDeclarationExpression ip in method.Parameters)
391                                         {
392                                                 if (ip.Name == cpd.Name && ip.Type.BaseType == cpd.Type.BaseType) {
393                                                         ip.Direction = FieldDirection.Ref;
394                                                         methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
395                                                         found = true;
396                                                         break;
397                                                 }
398                                         }
399                                         
400                                         if (found) continue;
401         
402                                         if (!hasReturn) 
403                                         {
404                                                 hasReturn = true;
405                                                 method.ReturnType = cpd.Type;
406                                                 methodEnd.ReturnType = cpd.Type;
407                                                 GenerateReturnAttributes (outputMembers, outputMembers[n], bodyBinding.Use, method);
408                                                 outParams [n] = null;
409                                                 continue;
410                                         }
411                                         
412                                         method.Parameters.Add (cpd);
413                                         GenerateMemberAttributes (outputMembers, outputMembers[n], bodyBinding.Use, cpd);
414                                         methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
415                                 }
416                         }
417                         
418                         methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
419                         methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
420                         methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
421
422                         // Array of input parameters
423                         
424                         CodeArrayCreateExpression methodParams;
425                         if (paramArray.Length > 0)
426                                 methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
427                         else
428                                 methodParams = new CodeArrayCreateExpression (typeof(object), 0);
429
430                         // Assignment of output parameters
431                         
432                         CodeStatementCollection outAssign = new CodeStatementCollection ();
433                         CodeVariableReferenceExpression arrVar = new CodeVariableReferenceExpression (varResults);
434                         for (int n=0; n<outParams.Length; n++)
435                         {
436                                 CodeExpression index = new CodePrimitiveExpression (n);
437                                 if (outParams[n] == null)
438                                 {
439                                         CodeExpression res = new CodeCastExpression (method.ReturnType, new CodeArrayIndexerExpression (arrVar, index));
440                                         outAssign.Add (new CodeMethodReturnStatement (res));
441                                 }
442                                 else
443                                 {
444                                         CodeExpression res = new CodeCastExpression (outParams[n].Type, new CodeArrayIndexerExpression (arrVar, index));
445                                         CodeExpression var = new CodeVariableReferenceExpression (outParams[n].Name);
446                                         outAssign.Insert (0, new CodeAssignStatement (var, res));
447                                 }
448                         }
449                         
450                         if (Style == ServiceDescriptionImportStyle.Client) 
451                         {
452                                 // Invoke call
453                                 
454                                 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
455                                 CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
456                                 CodeMethodInvokeExpression inv;
457                                 CodeVariableDeclarationStatement dec;
458         
459                                 inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, methodParams);
460                                 if (outputMembers != null && outputMembers.Count > 0)
461                                 {
462                                         dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
463                                         method.Statements.Add (dec);
464                                         method.Statements.AddRange (outAssign);
465                                 }
466                                 else
467                                         method.Statements.Add (inv);
468                                 
469                                 // Begin Invoke Call
470                                 
471                                 CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
472                                 CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
473                                 inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, methodParams, expCallb, expAsyncs);
474                                 methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
475                                 
476                                 // End Invoke call
477                                 
478                                 CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
479                                 inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
480                                 if (outputMembers != null && outputMembers.Count > 0)
481                                 {
482                                         dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
483                                         methodEnd.Statements.Add (dec);
484                                         methodEnd.Statements.AddRange (outAssign);
485                                 }
486                                 else
487                                         methodEnd.Statements.Add (inv);
488                         }
489                         else {
490                                 method.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
491                         }
492                         
493                         // Attributes
494                         
495                         ImportHeaders (method);
496                         
497                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebMethodAttribute");
498                         if (messageName != method.Name) att.Arguments.Add (GetArg ("MessageName",messageName));
499                         AddCustomAttribute (method, att, (Style == ServiceDescriptionImportStyle.Server));
500                         
501                         if (style == SoapBindingStyle.Rpc)
502                         {
503                                 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapRpcMethodAttribute");
504                                 att.Arguments.Add (GetArg (soapOper.SoapAction));
505                                 if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
506                                 if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
507                                 att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
508                                 if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
509                                 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
510                         }
511                         else
512                         {
513                                 if (outputMembers != null && (inputMembers.ElementName == "" && outputMembers.ElementName != "" || 
514                                         inputMembers.ElementName != "" && outputMembers.ElementName == ""))
515                                         throw new InvalidOperationException ("Parameter style is not the same for the input message and output message");
516         
517                                 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapDocumentMethodAttribute");
518                                 att.Arguments.Add (GetArg (soapOper.SoapAction));
519                                 if (inputMembers.ElementName != "") {
520                                         if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
521                                         if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
522                                         att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
523                                         if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
524                                         att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Wrapped"));
525                                 }
526                                 else
527                                         att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Bare"));
528                                         
529                                 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
530                                         
531                                 att.Arguments.Add (GetEnumArg ("Use", "System.Web.Services.Description.SoapBindingUse", bodyBinding.Use.ToString()));
532                         }
533                         
534                         AddCustomAttribute (method, att, true);
535                         
536                         CodeTypeDeclaration.Members.Add (method);
537                         
538                         if (Style == ServiceDescriptionImportStyle.Client) {
539                                 CodeTypeDeclaration.Members.Add (methodBegin);
540                                 CodeTypeDeclaration.Members.Add (methodEnd);
541                         }
542                         
543                         return method;
544                 }
545                 
546                 CodeParameterDeclarationExpression GenerateParameter (XmlMemberMapping member, FieldDirection dir)
547                 {
548                         string type = member.GenerateTypeName (CodeGenerator);
549                         CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression (type, member.MemberName);
550                         par.Direction = dir;
551                         return par;
552                 }
553                 
554                 void GenerateMemberAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeParameterDeclarationExpression param)
555                 {
556                         if (use == SoapBindingUse.Literal)
557                                 xmlExporter.AddMappingMetadata (param.CustomAttributes, member, members.Namespace);
558                         else
559                                 soapExporter.AddMappingMetadata (param.CustomAttributes, member);
560                 }
561                 
562                 void GenerateReturnAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeMemberMethod method)
563                 {
564                         if (use == SoapBindingUse.Literal)
565                                 xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, members.Namespace, (member.ElementName != method.Name + "Result"));
566                         else
567                                 soapExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, (member.ElementName != method.Name + "Result"));
568                 }
569                 
570                 void ImportHeaders (CodeMemberMethod method)
571                 {
572                         foreach (object ob in OperationBinding.Input.Extensions)
573                         {
574                                 SoapHeaderBinding hb = ob as SoapHeaderBinding;
575                                 if (hb == null) continue;
576                                 if (HasHeader (OperationBinding.Output, hb)) 
577                                         ImportHeader (method, hb, SoapHeaderDirection.In | SoapHeaderDirection.Out);
578                                 else
579                                         ImportHeader (method, hb, SoapHeaderDirection.In);
580                         }
581                         
582                         if (OperationBinding.Output == null) return;
583                         
584                         foreach (object ob in OperationBinding.Output.Extensions)
585                         {
586                                 SoapHeaderBinding hb = ob as SoapHeaderBinding;
587                                 if (hb == null) continue;
588                                 if (!HasHeader (OperationBinding.Input, hb)) 
589                                         ImportHeader (method, hb, SoapHeaderDirection.Out);
590                         }
591                 }
592                 
593                 bool HasHeader (MessageBinding msg, SoapHeaderBinding hb)
594                 {
595                         if (msg == null) return false;
596                         
597                         foreach (object ob in msg.Extensions) 
598                         {
599                                 SoapHeaderBinding mhb = ob as SoapHeaderBinding;
600                                 if ((mhb != null) && (mhb.Message == hb.Message) && (mhb.Part == hb.Part)) 
601                                         return true;
602                         }
603                         return false;
604                 }
605                 
606                 void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
607                 {
608                         Message msg = ServiceDescriptions.GetMessage (hb.Message);
609                         if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
610                         MessagePart part = msg.Parts [hb.Part];
611                         if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);
612
613                         XmlTypeMapping map;
614                         string hname;
615                         if (hb.Use == SoapBindingUse.Literal)
616                         {
617                                 map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
618                                 hname = part.Element.Name;
619                                 xmlExporter.ExportTypeMapping (map);
620                         }
621                         else
622                         {
623                                 map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
624                                 hname = part.Type.Name;
625                                 soapExporter.ExportTypeMapping (map);
626                         }
627
628                         string varName = headerVariables [map] as string;
629                         if (varName == null) 
630                         {
631                                 if (hname == map.TypeName)
632                                         varName = memberIds.AddUnique(CodeIdentifier.MakeValid (hname + "Value"),hb);
633                                 else
634                                         varName = memberIds.AddUnique(CodeIdentifier.MakeValid (hname),hb);
635                                 
636                                 string propName = varName;
637                                 varName = varName + "Field";
638                                 headerVariables.Add (map, varName);
639                                 CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
640                                 CodeTypeDeclaration.Members.Add (codeField);
641                                 
642                                 codeField.Attributes = MemberAttributes.Private;
643                                 CodeMemberProperty codeProperty = new CodeMemberProperty ();
644                                 codeProperty.Name = propName;
645                                 codeProperty.Type = new CodeTypeReference (map.TypeFullName);
646                                 codeProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
647                                 codeProperty.HasGet = codeProperty.HasSet = true;
648                                 CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), varName);
649                                 codeProperty.SetStatements.Add (new CodeAssignStatement (ce, new CodePropertySetValueReferenceExpression()));
650                                 codeProperty.GetStatements.Add (new CodeMethodReturnStatement (ce));
651                                 CodeTypeDeclaration.Members.Add (codeProperty);
652
653                                 varName = propName;
654                         }
655                         
656                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
657                         att.Arguments.Add (GetArg (varName));
658 #if ONLY_1_0
659                         att.Arguments.Add (GetArg ("Required", false));
660 #endif
661                         if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
662                         AddCustomAttribute (method, att, true);
663                 }
664                 
665                 #endregion
666         }
667
668         internal class Soap12ProtocolImporter : SoapProtocolImporter
669         {
670                 public override string ProtocolName {
671                         get { return "Soap12"; }
672                 }
673
674                 protected override bool IsBindingSupported ()
675                 {
676                         return Binding.Extensions.Find (typeof(Soap12Binding)) != null;
677                 }
678         }
679 }