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