2006-12-12 Atsushi Enomoto <atsushi@ximian.com>
[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                         
110                         string location = null;
111                         
112                         if (Port != null) {
113                                 SoapAddressBinding sab = (SoapAddressBinding) Port.Extensions.Find (typeof(SoapAddressBinding));
114                                 if (sab != null) location = sab.Location;
115                         }
116                         
117                         string namspace = (Port != null ? Port.Binding.Namespace : Binding.ServiceDescription.TargetNamespace);
118                         string name = (Port != null ? Port.Name : Binding.Name);
119
120                         if (Style == ServiceDescriptionImportStyle.Client) {
121                                 CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.Protocols.SoapHttpClientProtocol");
122                                 codeClass.BaseTypes.Add (ctr);
123                         }
124                         else {
125                                 CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.WebService");
126                                 codeClass.BaseTypes.Add (ctr);
127                                 CodeAttributeDeclaration attws = new CodeAttributeDeclaration ("System.Web.Services.WebServiceAttribute");
128                                 attws.Arguments.Add (GetArg ("Namespace", namspace));
129                                 AddCustomAttribute (codeClass, attws, true);
130                         }
131                         
132                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebServiceBinding");
133                         att.Arguments.Add (GetArg ("Name", name));
134                         att.Arguments.Add (GetArg ("Namespace", namspace));
135                         AddCustomAttribute (codeClass, att, true);
136         
137                         if (Style == ServiceDescriptionImportStyle.Client) {
138                                 CodeConstructor cc = new CodeConstructor ();
139                                 cc.Attributes = MemberAttributes.Public;
140                                 GenerateServiceUrl (location, cc.Statements);
141                                 codeClass.Members.Add (cc);
142                         }
143                         
144                         memberIds = new CodeIdentifiers ();
145                         headerVariables = new Hashtable ();
146                         return codeClass;
147                 }
148                 
149                 protected override void BeginNamespace ()
150                 {
151 #if NET_2_0
152                         xmlImporter = new XmlSchemaImporter (LiteralSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
153                         soapImporter = new SoapSchemaImporter (EncodedSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext);
154                         xmlExporter = new XmlCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
155                         soapExporter = new SoapCodeExporter (CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null);
156 #else
157                         xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
158                         soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
159                         xmlExporter = new XmlCodeExporter (CodeNamespace, null);
160                         soapExporter = new SoapCodeExporter (CodeNamespace, null);
161 #endif
162                 }
163
164                 protected override void EndClass ()
165                 {
166                         SoapTransportImporter transportImporter = SoapTransportImporter.FindTransportImporter (soapBinding.Transport);
167                         if (transportImporter == null) throw new InvalidOperationException ("Transport '" + soapBinding.Transport + "' not supported");
168                         transportImporter.ImportContext = this;
169                         transportImporter.ImportClass ();
170                         
171                         if (xmlExporter.IncludeMetadata.Count > 0 || soapExporter.IncludeMetadata.Count > 0)
172                         {
173                                 if (CodeTypeDeclaration.CustomAttributes == null)
174                                         CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
175                                 CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
176                                 CodeTypeDeclaration.CustomAttributes.AddRange (soapExporter.IncludeMetadata);
177                         }
178                 }
179
180                 protected override void EndNamespace ()
181                 {
182                 }
183
184                 protected override bool IsBindingSupported ()
185                 {
186                         return Binding.Extensions.Find (typeof(SoapBinding)) != null;
187                 }
188
189                 [MonoTODO]
190                 protected override bool IsOperationFlowSupported (OperationFlow flow)
191                 {
192                         throw new NotImplementedException ();
193                 }
194
195                 static readonly char [] whitespaces = new char [] {' ', '\t', '\n', '\r'};
196
197                 protected virtual bool IsSoapEncodingPresent (string uriList)
198                 {
199                         foreach (string s in uriList.Split (whitespaces))
200                                 if (s == "http://schemas.xmlsoap.org/soap/encoding/")
201                                         return true;
202                         return false;
203                 }
204
205                 protected override CodeMemberMethod GenerateMethod ()
206                 {
207                         try
208                         {
209                                 SoapOperationBinding soapOper = OperationBinding.Extensions.Find (typeof (SoapOperationBinding)) as SoapOperationBinding;
210                                 if (soapOper == null) throw new InvalidOperationException ("Soap operation binding not found");
211
212                                 SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
213                         
214                                 SoapBodyBinding isbb = null;
215                                 XmlMembersMapping inputMembers = null;
216                                 
217                                 bool isWrapped = CheckIsWrapped ();
218                                 
219                                 isbb = OperationBinding.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
220                                 if (isbb == null) throw new InvalidOperationException ("Soap body binding not found");
221                         
222                                 inputMembers = ImportMembersMapping (InputMessage, isbb, style, false, isWrapped);
223                                 if (inputMembers == null) throw new InvalidOperationException ("Input message not declared");
224                                 
225                                 // If OperationBinding.Output is null, it is an OneWay operation
226                                 
227                                 SoapBodyBinding osbb = null;
228                                 XmlMembersMapping outputMembers = null;
229                                 
230                                 if (OperationBinding.Output != null) {
231                                         osbb = OperationBinding.Output.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
232                                         if (osbb == null) throw new InvalidOperationException ("Soap body binding not found");
233
234                                         outputMembers = ImportMembersMapping (OutputMessage, osbb, style, true, isWrapped);
235                                         if (outputMembers == null) throw new InvalidOperationException ("Output message not declared");
236                                 }
237                                 
238                                 CodeMemberMethod met = GenerateMethod (memberIds, soapOper, isbb, inputMembers, outputMembers);
239                                 
240                                 if (isbb.Use == SoapBindingUse.Literal)
241                                         xmlExporter.ExportMembersMapping (inputMembers);
242                                 else
243                                         soapExporter.ExportMembersMapping (inputMembers);
244                                 
245                                 if (osbb != null) {
246                                         if (osbb.Use == SoapBindingUse.Literal)
247                                                 xmlExporter.ExportMembersMapping (outputMembers);
248                                         else
249                                                 soapExporter.ExportMembersMapping (outputMembers);
250                                 }
251                                 
252                                 foreach (SoapExtensionImporter eximporter in extensionImporters)
253                                 {
254                                         eximporter.ImportContext = this;
255                                         eximporter.ImportMethod (met.CustomAttributes);
256                                 }
257                                 
258                                 return met;
259                         }
260                         catch (InvalidOperationException ex)
261                         {
262                                 UnsupportedOperationBindingWarning (ex.Message);
263                                 return null;
264                         }
265                 }
266                 
267                 bool CheckIsWrapped ()
268                 {
269                         return (OutputMessage == null || (OutputMessage.Parts.Count == 1 && OutputMessage.Parts[0].Name == "parameters")) &&
270                                    (InputMessage == null || (InputMessage.Parts.Count == 1 && InputMessage.Parts[0].Name == "parameters"));
271                 }
272                 
273                 XmlMembersMapping ImportMembersMapping (Message msg, SoapBodyBinding sbb, SoapBindingStyle style, bool output, bool wrapped)
274                 {
275                         string elemName = Operation.Name;
276                         if (output) elemName += "Response";
277
278                         if (wrapped)
279                         {
280                                 // Wrapped parameter style
281                                 
282                                 MessagePart part = msg.Parts[0];
283                                 if (sbb.Use == SoapBindingUse.Encoded)
284                                 {
285                                         SoapSchemaMember ssm = new SoapSchemaMember ();
286                                         ssm.MemberName = part.Name;
287                                         ssm.MemberType = part.Type;
288                                         return soapImporter.ImportMembersMapping (elemName, part.Type.Namespace, ssm);
289                                 }
290                                 else
291                                         return xmlImporter.ImportMembersMapping (part.Element);
292                         }
293                         else
294                         {
295                                 if (sbb.Use == SoapBindingUse.Encoded)
296                                 {
297                                         SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
298                                         for (int n=0; n<mems.Length; n++)
299                                         {
300                                                 SoapSchemaMember mem = new SoapSchemaMember();
301                                                 mem.MemberName = msg.Parts[n].Name;
302                                                 mem.MemberType = msg.Parts[n].Type;
303                                                 mems[n] = mem;
304                                         }
305                                         
306                                         // Rpc messages always have a wrapping element
307                                         if (style == SoapBindingStyle.Rpc)
308                                                 return soapImporter.ImportMembersMapping (elemName, sbb.Namespace, mems, true);
309                                         else
310                                                 return soapImporter.ImportMembersMapping ("", "", mems, false);
311                                 }
312                                 else
313                                 {
314                                         if (style == SoapBindingStyle.Rpc)
315                                                 throw new InvalidOperationException ("The combination of style=rpc with use=literal is not supported");
316                                         
317                                         if (msg.Parts.Count == 1 && msg.Parts[0].Type != XmlQualifiedName.Empty)
318                                                 return xmlImporter.ImportAnyType (msg.Parts[0].Type, null);
319                                         else
320                                         {
321                                                 XmlQualifiedName[] pnames = new XmlQualifiedName [msg.Parts.Count];
322                                                 for (int n=0; n<pnames.Length; n++)
323                                                         pnames[n] = msg.Parts[n].Element;
324                                                 return xmlImporter.ImportMembersMapping (pnames);
325                                         }
326                                 }
327                         }
328                 }
329                 
330                 CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, SoapOperationBinding soapOper, SoapBodyBinding bodyBinding, XmlMembersMapping inputMembers, XmlMembersMapping outputMembers)
331                 {
332                         CodeIdentifiers pids = new CodeIdentifiers ();
333                         CodeMemberMethod method = new CodeMemberMethod ();
334                         CodeMemberMethod methodBegin = new CodeMemberMethod ();
335                         CodeMemberMethod methodEnd = new CodeMemberMethod ();
336                         method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
337                         methodBegin.Attributes = MemberAttributes.Public | MemberAttributes.Final;
338                         methodEnd.Attributes = MemberAttributes.Public | MemberAttributes.Final;
339                         
340                         SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
341                         
342                         // Find unique names for temporary variables
343                         
344                         for (int n=0; n<inputMembers.Count; n++)
345                                 pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
346
347                         if (outputMembers != null)
348                                 for (int n=0; n<outputMembers.Count; n++)
349                                         pids.AddUnique (outputMembers[n].MemberName, outputMembers[n]);
350                                 
351                         string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
352                         string varResults = pids.AddUnique ("results","results");
353                         string varCallback = pids.AddUnique ("callback","callback");
354                         string varAsyncState = pids.AddUnique ("asyncState","asyncState");
355
356                         string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
357
358                         method.Name = CodeIdentifier.MakeValid(Operation.Name);
359                         if (method.Name == ClassName) method.Name += "1";
360                         methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + method.Name),method);
361                         methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + method.Name),method);
362
363                         method.ReturnType = new CodeTypeReference (typeof(void));
364                         methodEnd.ReturnType = new CodeTypeReference (typeof(void));
365                         methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
366
367                         CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
368                         CodeParameterDeclarationExpression[] outParams = new CodeParameterDeclarationExpression [outputMembers != null ? outputMembers.Count : 0];
369
370                         for (int n=0; n<inputMembers.Count; n++)
371                         {
372                                 CodeParameterDeclarationExpression param = GenerateParameter (inputMembers[n], FieldDirection.In);
373                                 method.Parameters.Add (param);
374                                 GenerateMemberAttributes (inputMembers, inputMembers[n], bodyBinding.Use, param);
375                                 methodBegin.Parameters.Add (GenerateParameter (inputMembers[n], FieldDirection.In));
376                                 paramArray [n] = new CodeVariableReferenceExpression (param.Name);
377                         }
378
379                         if (outputMembers != null)
380                         {
381                                 bool hasReturn = false;
382                                 for (int n=0; n<outputMembers.Count; n++)
383                                 {
384                                         CodeParameterDeclarationExpression cpd = GenerateParameter (outputMembers[n], FieldDirection.Out);
385                                         outParams [n] = cpd;
386                                         
387                                         bool found = false;
388                                         foreach (CodeParameterDeclarationExpression ip in method.Parameters)
389                                         {
390                                                 if (ip.Name == cpd.Name && ip.Type.BaseType == cpd.Type.BaseType) {
391                                                         ip.Direction = FieldDirection.Ref;
392                                                         methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
393                                                         found = true;
394                                                         break;
395                                                 }
396                                         }
397                                         
398                                         if (found) continue;
399         
400                                         if (!hasReturn) 
401                                         {
402                                                 hasReturn = true;
403                                                 method.ReturnType = cpd.Type;
404                                                 methodEnd.ReturnType = cpd.Type;
405                                                 GenerateReturnAttributes (outputMembers, outputMembers[n], bodyBinding.Use, method);
406                                                 outParams [n] = null;
407                                                 continue;
408                                         }
409                                         
410                                         method.Parameters.Add (cpd);
411                                         GenerateMemberAttributes (outputMembers, outputMembers[n], bodyBinding.Use, cpd);
412                                         methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
413                                 }
414                         }
415                         
416                         methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
417                         methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
418                         methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
419
420                         // Array of input parameters
421                         
422                         CodeArrayCreateExpression methodParams;
423                         if (paramArray.Length > 0)
424                                 methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
425                         else
426                                 methodParams = new CodeArrayCreateExpression (typeof(object), 0);
427
428                         // Assignment of output parameters
429                         
430                         CodeStatementCollection outAssign = new CodeStatementCollection ();
431                         CodeVariableReferenceExpression arrVar = new CodeVariableReferenceExpression (varResults);
432                         for (int n=0; n<outParams.Length; n++)
433                         {
434                                 CodeExpression index = new CodePrimitiveExpression (n);
435                                 if (outParams[n] == null)
436                                 {
437                                         CodeExpression res = new CodeCastExpression (method.ReturnType, new CodeArrayIndexerExpression (arrVar, index));
438                                         outAssign.Add (new CodeMethodReturnStatement (res));
439                                 }
440                                 else
441                                 {
442                                         CodeExpression res = new CodeCastExpression (outParams[n].Type, new CodeArrayIndexerExpression (arrVar, index));
443                                         CodeExpression var = new CodeVariableReferenceExpression (outParams[n].Name);
444                                         outAssign.Insert (0, new CodeAssignStatement (var, res));
445                                 }
446                         }
447                         
448                         if (Style == ServiceDescriptionImportStyle.Client) 
449                         {
450                                 // Invoke call
451                                 
452                                 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
453                                 CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
454                                 CodeMethodInvokeExpression inv;
455                                 CodeVariableDeclarationStatement dec;
456         
457                                 inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, methodParams);
458                                 if (outputMembers != null && outputMembers.Count > 0)
459                                 {
460                                         dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
461                                         method.Statements.Add (dec);
462                                         method.Statements.AddRange (outAssign);
463                                 }
464                                 else
465                                         method.Statements.Add (inv);
466                                 
467                                 // Begin Invoke Call
468                                 
469                                 CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
470                                 CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
471                                 inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, methodParams, expCallb, expAsyncs);
472                                 methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
473                                 
474                                 // End Invoke call
475                                 
476                                 CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
477                                 inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
478                                 if (outputMembers != null && outputMembers.Count > 0)
479                                 {
480                                         dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
481                                         methodEnd.Statements.Add (dec);
482                                         methodEnd.Statements.AddRange (outAssign);
483                                 }
484                                 else
485                                         methodEnd.Statements.Add (inv);
486                         }
487                         else {
488                                 method.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
489                         }
490                         
491                         // Attributes
492                         
493                         ImportHeaders (method);
494                         
495                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebMethodAttribute");
496                         if (messageName != method.Name) att.Arguments.Add (GetArg ("MessageName",messageName));
497                         AddCustomAttribute (method, att, (Style == ServiceDescriptionImportStyle.Server));
498                         
499                         if (style == SoapBindingStyle.Rpc)
500                         {
501                                 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapRpcMethodAttribute");
502                                 att.Arguments.Add (GetArg (soapOper.SoapAction));
503                                 if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
504                                 if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
505                                 att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
506                                 if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
507                                 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
508                         }
509                         else
510                         {
511                                 if (outputMembers != null && (inputMembers.ElementName == "" && outputMembers.ElementName != "" || 
512                                         inputMembers.ElementName != "" && outputMembers.ElementName == ""))
513                                         throw new InvalidOperationException ("Parameter style is not the same for the input message and output message");
514         
515                                 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapDocumentMethodAttribute");
516                                 att.Arguments.Add (GetArg (soapOper.SoapAction));
517                                 if (inputMembers.ElementName != "") {
518                                         if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
519                                         if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
520                                         att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
521                                         if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
522                                         att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Wrapped"));
523                                 }
524                                 else
525                                         att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Bare"));
526                                         
527                                 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
528                                         
529                                 att.Arguments.Add (GetEnumArg ("Use", "System.Web.Services.Description.SoapBindingUse", bodyBinding.Use.ToString()));
530                         }
531                         
532                         AddCustomAttribute (method, att, true);
533                         
534                         CodeTypeDeclaration.Members.Add (method);
535                         
536                         if (Style == ServiceDescriptionImportStyle.Client) {
537                                 CodeTypeDeclaration.Members.Add (methodBegin);
538                                 CodeTypeDeclaration.Members.Add (methodEnd);
539                         }
540                         
541                         return method;
542                 }
543                 
544                 CodeParameterDeclarationExpression GenerateParameter (XmlMemberMapping member, FieldDirection dir)
545                 {
546                         CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression (member.TypeFullName, member.MemberName);
547                         par.Direction = dir;
548                         return par;
549                 }
550                 
551                 void GenerateMemberAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeParameterDeclarationExpression param)
552                 {
553                         if (use == SoapBindingUse.Literal)
554                                 xmlExporter.AddMappingMetadata (param.CustomAttributes, member, members.Namespace);
555                         else
556                                 soapExporter.AddMappingMetadata (param.CustomAttributes, member);
557                 }
558                 
559                 void GenerateReturnAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeMemberMethod method)
560                 {
561                         if (use == SoapBindingUse.Literal)
562                                 xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, members.Namespace, (member.ElementName != method.Name + "Result"));
563                         else
564                                 soapExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, (member.ElementName != method.Name + "Result"));
565                 }
566                 
567                 void ImportHeaders (CodeMemberMethod method)
568                 {
569                         foreach (object ob in OperationBinding.Input.Extensions)
570                         {
571                                 SoapHeaderBinding hb = ob as SoapHeaderBinding;
572                                 if (hb == null) continue;
573                                 if (HasHeader (OperationBinding.Output, hb)) 
574                                         ImportHeader (method, hb, SoapHeaderDirection.In | SoapHeaderDirection.Out);
575                                 else
576                                         ImportHeader (method, hb, SoapHeaderDirection.In);
577                         }
578                         
579                         if (OperationBinding.Output == null) return;
580                         
581                         foreach (object ob in OperationBinding.Output.Extensions)
582                         {
583                                 SoapHeaderBinding hb = ob as SoapHeaderBinding;
584                                 if (hb == null) continue;
585                                 if (!HasHeader (OperationBinding.Input, hb)) 
586                                         ImportHeader (method, hb, SoapHeaderDirection.Out);
587                         }
588                 }
589                 
590                 bool HasHeader (MessageBinding msg, SoapHeaderBinding hb)
591                 {
592                         if (msg == null) return false;
593                         
594                         foreach (object ob in msg.Extensions) 
595                         {
596                                 SoapHeaderBinding mhb = ob as SoapHeaderBinding;
597                                 if ((mhb != null) && (mhb.Message == hb.Message) && (mhb.Part == hb.Part)) 
598                                         return true;
599                         }
600                         return false;
601                 }
602                 
603                 void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
604                 {
605                         Message msg = ServiceDescriptions.GetMessage (hb.Message);
606                         if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
607                         MessagePart part = msg.Parts [hb.Part];
608                         if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);
609
610                         XmlTypeMapping map;
611                         if (hb.Use == SoapBindingUse.Literal)
612                         {
613                                 map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
614                                 xmlExporter.ExportTypeMapping (map);
615                         }
616                         else
617                         {
618                                 map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
619                                 soapExporter.ExportTypeMapping (map);
620                         }
621
622                         bool required = false;
623
624                         string varName = headerVariables [map] as string;
625                         if (varName == null) 
626                         {
627                                 varName = memberIds.AddUnique(CodeIdentifier.MakeValid (map.TypeName + "Value"),hb);
628                                 headerVariables.Add (map, varName);
629                                 CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
630                                 codeField.Attributes = MemberAttributes.Public;
631                                 CodeTypeDeclaration.Members.Add (codeField);
632                         }
633                         
634                         CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
635                         att.Arguments.Add (GetArg (varName));
636                         att.Arguments.Add (GetArg ("Required", required));
637                         if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
638                         AddCustomAttribute (method, att, true);
639                 }
640                 
641                 #endregion
642         }
643 }