2 // System.Web.Services.Description.SoapProtocolImporter.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Web.Services;
34 using System.Web.Services.Protocols;
35 using System.Web.Services.Configuration;
37 using System.Xml.Schema;
38 using System.Xml.Serialization;
39 using System.Configuration;
40 using System.Collections;
42 namespace System.Web.Services.Description {
43 public class SoapProtocolImporter : ProtocolImporter {
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;
62 public SoapProtocolImporter ()
64 extensionImporters = ExtensionManager.BuildExtensionImporters ();
67 void SetBinding (SoapBinding soapBinding)
69 this.soapBinding = soapBinding;
72 #endregion // Constructors
76 public override string ProtocolName {
77 get { return "Soap"; }
80 public SoapBinding SoapBinding {
81 get { return soapBinding; }
84 public SoapCodeExporter SoapExporter {
85 get { return soapExporter; }
88 public SoapSchemaImporter SoapImporter {
89 get { return soapImporter; }
92 public XmlCodeExporter XmlExporter {
93 get { return xmlExporter; }
96 public XmlSchemaImporter XmlImporter {
97 get { return xmlImporter; }
100 #endregion // Properties
104 protected override CodeTypeDeclaration BeginClass ()
106 soapBinding = (SoapBinding) Binding.Extensions.Find (typeof(SoapBinding));
108 CodeTypeDeclaration codeClass = new CodeTypeDeclaration (ClassName);
110 string location = null;
111 SoapAddressBinding sab = (SoapAddressBinding) Port.Extensions.Find (typeof(SoapAddressBinding));
112 if (sab != null) location = sab.Location;
113 string url = GetServiceUrl (location);
115 CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.Protocols.SoapHttpClientProtocol");
116 codeClass.BaseTypes.Add (ctr);
118 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebServiceBinding");
119 att.Arguments.Add (GetArg ("Name", Port.Name));
120 att.Arguments.Add (GetArg ("Namespace", Port.Binding.Namespace));
121 AddCustomAttribute (codeClass, att, true);
123 CodeConstructor cc = new CodeConstructor ();
124 cc.Attributes = MemberAttributes.Public;
125 CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
126 CodeAssignStatement cas = new CodeAssignStatement (ce, new CodePrimitiveExpression (url));
127 cc.Statements.Add (cas);
128 codeClass.Members.Add (cc);
130 memberIds = new CodeIdentifiers ();
131 headerVariables = new Hashtable ();
135 protected override void BeginNamespace ()
137 xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
138 soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
139 xmlExporter = new XmlCodeExporter (CodeNamespace, null);
140 soapExporter = new SoapCodeExporter (CodeNamespace, null);
143 protected override void EndClass ()
145 SoapTransportImporter transportImporter = SoapTransportImporter.FindTransportImporter (soapBinding.Transport);
146 if (transportImporter == null) throw new InvalidOperationException ("Transport '" + soapBinding.Transport + "' not supported");
147 transportImporter.ImportContext = this;
148 transportImporter.ImportClass ();
150 if (xmlExporter.IncludeMetadata.Count > 0 || soapExporter.IncludeMetadata.Count > 0)
152 if (CodeTypeDeclaration.CustomAttributes == null)
153 CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
154 CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
155 CodeTypeDeclaration.CustomAttributes.AddRange (soapExporter.IncludeMetadata);
159 protected override void EndNamespace ()
163 protected override bool IsBindingSupported ()
165 return Binding.Extensions.Find (typeof(SoapBinding)) != null;
169 protected override bool IsOperationFlowSupported (OperationFlow flow)
171 throw new NotImplementedException ();
175 protected virtual bool IsSoapEncodingPresent (string uriList)
177 throw new NotImplementedException ();
180 protected override CodeMemberMethod GenerateMethod ()
184 SoapOperationBinding soapOper = OperationBinding.Extensions.Find (typeof (SoapOperationBinding)) as SoapOperationBinding;
185 if (soapOper == null) throw new InvalidOperationException ("Soap operation binding not found");
187 SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
189 SoapBodyBinding isbb = null;
190 XmlMembersMapping inputMembers = null;
192 isbb = OperationBinding.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
193 if (isbb == null) throw new InvalidOperationException ("Soap body binding not found");
195 inputMembers = ImportMembersMapping (InputMessage, isbb, style, false);
196 if (inputMembers == null) throw new InvalidOperationException ("Input message not declared");
198 // If OperationBinding.Output is null, it is an OneWay operation
200 SoapBodyBinding osbb = null;
201 XmlMembersMapping outputMembers = null;
203 if (OperationBinding.Output != null) {
204 osbb = OperationBinding.Output.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
205 if (osbb == null) throw new InvalidOperationException ("Soap body binding not found");
207 outputMembers = ImportMembersMapping (OutputMessage, osbb, style, true);
208 if (outputMembers == null) throw new InvalidOperationException ("Output message not declared");
211 CodeMemberMethod met = GenerateMethod (memberIds, soapOper, isbb, inputMembers, outputMembers);
213 if (isbb.Use == SoapBindingUse.Literal)
214 xmlExporter.ExportMembersMapping (inputMembers);
216 soapExporter.ExportMembersMapping (inputMembers);
219 if (osbb.Use == SoapBindingUse.Literal)
220 xmlExporter.ExportMembersMapping (outputMembers);
222 soapExporter.ExportMembersMapping (outputMembers);
225 foreach (SoapExtensionImporter eximporter in extensionImporters)
227 eximporter.ImportContext = this;
228 eximporter.ImportMethod (met.CustomAttributes);
233 catch (InvalidOperationException ex)
235 UnsupportedOperationBindingWarning (ex.Message);
240 XmlMembersMapping ImportMembersMapping (Message msg, SoapBodyBinding sbb, SoapBindingStyle style, bool output)
242 string elemName = Operation.Name;
243 if (output) elemName += "Response";
245 if (msg.Parts.Count == 1 && msg.Parts[0].Name == "parameters")
247 // Wrapped parameter style
249 MessagePart part = msg.Parts[0];
250 if (sbb.Use == SoapBindingUse.Encoded)
252 SoapSchemaMember ssm = new SoapSchemaMember ();
253 ssm.MemberName = part.Name;
254 ssm.MemberType = part.Type;
255 return soapImporter.ImportMembersMapping (elemName, part.Type.Namespace, ssm);
258 return xmlImporter.ImportMembersMapping (part.Element);
262 if (sbb.Use == SoapBindingUse.Encoded)
264 SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
265 for (int n=0; n<mems.Length; n++)
267 SoapSchemaMember mem = new SoapSchemaMember();
268 mem.MemberName = msg.Parts[n].Name;
269 mem.MemberType = msg.Parts[n].Type;
273 // Rpc messages always have a wrapping element
274 if (style == SoapBindingStyle.Rpc)
275 return soapImporter.ImportMembersMapping (elemName, sbb.Namespace, mems, true);
277 return soapImporter.ImportMembersMapping ("", "", mems, false);
281 if (style == SoapBindingStyle.Rpc)
282 throw new InvalidOperationException ("The combination of style=rpc with use=literal is not supported");
284 if (msg.Parts.Count == 1 && msg.Parts[0].Type != XmlQualifiedName.Empty)
285 return xmlImporter.ImportAnyType (msg.Parts[0].Type, null);
288 XmlQualifiedName[] pnames = new XmlQualifiedName [msg.Parts.Count];
289 for (int n=0; n<pnames.Length; n++)
290 pnames[n] = msg.Parts[n].Element;
291 return xmlImporter.ImportMembersMapping (pnames);
297 CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, SoapOperationBinding soapOper, SoapBodyBinding bodyBinding, XmlMembersMapping inputMembers, XmlMembersMapping outputMembers)
299 CodeIdentifiers pids = new CodeIdentifiers ();
300 CodeMemberMethod method = new CodeMemberMethod ();
301 CodeMemberMethod methodBegin = new CodeMemberMethod ();
302 CodeMemberMethod methodEnd = new CodeMemberMethod ();
303 method.Attributes = MemberAttributes.Public;
304 methodBegin.Attributes = MemberAttributes.Public;
305 methodEnd.Attributes = MemberAttributes.Public;
307 SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
309 // Find unique names for temporary variables
311 for (int n=0; n<inputMembers.Count; n++)
312 pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
314 if (outputMembers != null)
315 for (int n=0; n<outputMembers.Count; n++)
316 pids.AddUnique (outputMembers[n].MemberName, outputMembers[n]);
318 string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
319 string varResults = pids.AddUnique ("results","results");
320 string varCallback = pids.AddUnique ("callback","callback");
321 string varAsyncState = pids.AddUnique ("asyncState","asyncState");
323 string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
325 method.Name = CodeIdentifier.MakeValid(Operation.Name);
326 if (method.Name == ClassName) method.Name += "1";
327 methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + method.Name),method);
328 methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + method.Name),method);
330 method.ReturnType = new CodeTypeReference (typeof(void));
331 methodEnd.ReturnType = new CodeTypeReference (typeof(void));
332 methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
334 CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
335 CodeParameterDeclarationExpression[] outParams = new CodeParameterDeclarationExpression [outputMembers != null ? outputMembers.Count : 0];
337 for (int n=0; n<inputMembers.Count; n++)
339 CodeParameterDeclarationExpression param = GenerateParameter (inputMembers[n], FieldDirection.In);
340 method.Parameters.Add (param);
341 GenerateMemberAttributes (inputMembers, inputMembers[n], bodyBinding.Use, param);
342 methodBegin.Parameters.Add (GenerateParameter (inputMembers[n], FieldDirection.In));
343 paramArray [n] = new CodeVariableReferenceExpression (param.Name);
346 if (outputMembers != null)
348 bool hasReturn = false;
349 for (int n=0; n<outputMembers.Count; n++)
351 CodeParameterDeclarationExpression cpd = GenerateParameter (outputMembers[n], FieldDirection.Out);
355 foreach (CodeParameterDeclarationExpression ip in method.Parameters)
357 if (ip.Name == cpd.Name && ip.Type.BaseType == cpd.Type.BaseType) {
358 ip.Direction = FieldDirection.Ref;
359 methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
370 method.ReturnType = cpd.Type;
371 methodEnd.ReturnType = cpd.Type;
372 GenerateReturnAttributes (outputMembers, outputMembers[n], bodyBinding.Use, method);
373 outParams [n] = null;
377 method.Parameters.Add (cpd);
378 GenerateMemberAttributes (outputMembers, outputMembers[n], bodyBinding.Use, cpd);
379 methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
383 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
384 methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
385 methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
387 // Array of input parameters
389 CodeArrayCreateExpression methodParams;
390 if (paramArray.Length > 0)
391 methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
393 methodParams = new CodeArrayCreateExpression (typeof(object), 0);
395 // Assignment of output parameters
397 CodeStatementCollection outAssign = new CodeStatementCollection ();
398 CodeVariableReferenceExpression arrVar = new CodeVariableReferenceExpression (varResults);
399 for (int n=0; n<outParams.Length; n++)
401 CodeExpression index = new CodePrimitiveExpression (n);
402 if (outParams[n] == null)
404 CodeExpression res = new CodeCastExpression (method.ReturnType, new CodeArrayIndexerExpression (arrVar, index));
405 outAssign.Add (new CodeMethodReturnStatement (res));
409 CodeExpression res = new CodeCastExpression (outParams[n].Type, new CodeArrayIndexerExpression (arrVar, index));
410 CodeExpression var = new CodeVariableReferenceExpression (outParams[n].Name);
411 outAssign.Insert (0, new CodeAssignStatement (var, res));
417 CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
418 CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
419 CodeMethodInvokeExpression inv;
420 CodeVariableDeclarationStatement dec;
422 inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, methodParams);
423 if (outputMembers != null && outputMembers.Count > 0)
425 dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
426 method.Statements.Add (dec);
427 method.Statements.AddRange (outAssign);
430 method.Statements.Add (inv);
434 CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
435 CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
436 inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, methodParams, expCallb, expAsyncs);
437 methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
441 CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
442 inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
443 if (outputMembers != null && outputMembers.Count > 0)
445 dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
446 methodEnd.Statements.Add (dec);
447 methodEnd.Statements.AddRange (outAssign);
450 methodEnd.Statements.Add (inv);
454 ImportHeaders (method);
456 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebMethodAttribute");
457 if (messageName != method.Name) att.Arguments.Add (GetArg ("MessageName",messageName));
458 AddCustomAttribute (method, att, false);
460 if (style == SoapBindingStyle.Rpc)
462 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapRpcMethodAttribute");
463 att.Arguments.Add (GetArg (soapOper.SoapAction));
464 if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
465 if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
466 att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
467 if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
468 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
472 if (outputMembers != null && (inputMembers.ElementName == "" && outputMembers.ElementName != "" ||
473 inputMembers.ElementName != "" && outputMembers.ElementName == ""))
474 throw new InvalidOperationException ("Parameter style is not the same for the input message and output message");
476 att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapDocumentMethodAttribute");
477 att.Arguments.Add (GetArg (soapOper.SoapAction));
478 if (inputMembers.ElementName != "") {
479 if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
480 if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
481 att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
482 if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
483 att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Wrapped"));
486 att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Bare"));
488 if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
490 att.Arguments.Add (GetEnumArg ("Use", "System.Web.Services.Description.SoapBindingUse", bodyBinding.Use.ToString()));
493 AddCustomAttribute (method, att, true);
495 CodeTypeDeclaration.Members.Add (method);
496 CodeTypeDeclaration.Members.Add (methodBegin);
497 CodeTypeDeclaration.Members.Add (methodEnd);
502 CodeParameterDeclarationExpression GenerateParameter (XmlMemberMapping member, FieldDirection dir)
504 CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression (member.TypeFullName, member.MemberName);
509 void GenerateMemberAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeParameterDeclarationExpression param)
511 if (use == SoapBindingUse.Literal)
512 xmlExporter.AddMappingMetadata (param.CustomAttributes, member, members.Namespace);
514 soapExporter.AddMappingMetadata (param.CustomAttributes, member);
517 void GenerateReturnAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeMemberMethod method)
519 if (use == SoapBindingUse.Literal)
520 xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, members.Namespace, (member.ElementName != method.Name + "Result"));
522 soapExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, (member.ElementName != method.Name + "Result"));
525 void ImportHeaders (CodeMemberMethod method)
527 foreach (object ob in OperationBinding.Input.Extensions)
529 SoapHeaderBinding hb = ob as SoapHeaderBinding;
530 if (hb == null) continue;
531 if (HasHeader (OperationBinding.Output, hb))
532 ImportHeader (method, hb, SoapHeaderDirection.In | SoapHeaderDirection.Out);
534 ImportHeader (method, hb, SoapHeaderDirection.In);
537 if (OperationBinding.Output == null) return;
539 foreach (object ob in OperationBinding.Output.Extensions)
541 SoapHeaderBinding hb = ob as SoapHeaderBinding;
542 if (hb == null) continue;
543 if (!HasHeader (OperationBinding.Input, hb))
544 ImportHeader (method, hb, SoapHeaderDirection.Out);
548 bool HasHeader (MessageBinding msg, SoapHeaderBinding hb)
550 if (msg == null) return false;
552 foreach (object ob in msg.Extensions)
554 SoapHeaderBinding mhb = ob as SoapHeaderBinding;
555 if ((mhb != null) && (mhb.Message == hb.Message) && (mhb.Part == hb.Part))
561 void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
563 Message msg = ServiceDescriptions.GetMessage (hb.Message);
564 if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
565 MessagePart part = msg.Parts [hb.Part];
566 if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);
569 if (hb.Use == SoapBindingUse.Literal)
571 map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
572 xmlExporter.ExportTypeMapping (map);
576 map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
577 soapExporter.ExportTypeMapping (map);
580 bool required = false;
582 string varName = headerVariables [map] as string;
585 varName = memberIds.AddUnique(CodeIdentifier.MakeValid (map.TypeName + "Value"),hb);
586 headerVariables.Add (map, varName);
587 CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
588 codeField.Attributes = MemberAttributes.Public;
589 CodeTypeDeclaration.Members.Add (codeField);
592 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
593 att.Arguments.Add (GetArg (varName));
594 att.Arguments.Add (GetArg ("Required", required));
595 if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
596 AddCustomAttribute (method, att, true);