2
0

SoapProtocolImporter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. //
  2. // System.Web.Services.Description.SoapProtocolImporter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.CodeDom;
  31. using System.Web.Services;
  32. using System.Web.Services.Protocols;
  33. using System.Web.Services.Configuration;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Xml.Serialization;
  37. using System.Configuration;
  38. using System.Collections;
  39. namespace System.Web.Services.Description {
  40. public class SoapProtocolImporter : ProtocolImporter {
  41. #region Fields
  42. SoapBinding soapBinding;
  43. SoapCodeExporter soapExporter;
  44. SoapSchemaImporter soapImporter;
  45. XmlCodeExporter xmlExporter;
  46. XmlSchemaImporter xmlImporter;
  47. CodeIdentifiers memberIds;
  48. ArrayList extensionImporters;
  49. Hashtable headerVariables;
  50. XmlSchemas xmlSchemas;
  51. XmlSchemas soapSchemas;
  52. #endregion // Fields
  53. #region Constructors
  54. public SoapProtocolImporter ()
  55. {
  56. extensionImporters = ExtensionManager.BuildExtensionImporters ();
  57. }
  58. void SetBinding (SoapBinding soapBinding)
  59. {
  60. this.soapBinding = soapBinding;
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. public override string ProtocolName {
  65. get { return "Soap"; }
  66. }
  67. public SoapBinding SoapBinding {
  68. get { return soapBinding; }
  69. }
  70. public SoapCodeExporter SoapExporter {
  71. get { return soapExporter; }
  72. }
  73. public SoapSchemaImporter SoapImporter {
  74. get { return soapImporter; }
  75. }
  76. public XmlCodeExporter XmlExporter {
  77. get { return xmlExporter; }
  78. }
  79. public XmlSchemaImporter XmlImporter {
  80. get { return xmlImporter; }
  81. }
  82. #endregion // Properties
  83. #region Methods
  84. protected override CodeTypeDeclaration BeginClass ()
  85. {
  86. soapBinding = (SoapBinding) Binding.Extensions.Find (typeof(SoapBinding));
  87. CodeTypeDeclaration codeClass = new CodeTypeDeclaration (ClassName);
  88. string location = null;
  89. SoapAddressBinding sab = (SoapAddressBinding) Port.Extensions.Find (typeof(SoapAddressBinding));
  90. if (sab != null) location = sab.Location;
  91. string url = GetServiceUrl (location);
  92. CodeTypeReference ctr = new CodeTypeReference ("System.Web.Services.Protocols.SoapHttpClientProtocol");
  93. codeClass.BaseTypes.Add (ctr);
  94. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebServiceBinding");
  95. att.Arguments.Add (GetArg ("Name", Port.Name));
  96. att.Arguments.Add (GetArg ("Namespace", Port.Binding.Namespace));
  97. AddCustomAttribute (codeClass, att, true);
  98. CodeConstructor cc = new CodeConstructor ();
  99. cc.Attributes = MemberAttributes.Public;
  100. CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
  101. CodeAssignStatement cas = new CodeAssignStatement (ce, new CodePrimitiveExpression (url));
  102. cc.Statements.Add (cas);
  103. codeClass.Members.Add (cc);
  104. memberIds = new CodeIdentifiers ();
  105. headerVariables = new Hashtable ();
  106. return codeClass;
  107. }
  108. protected override void BeginNamespace ()
  109. {
  110. xmlImporter = new XmlSchemaImporter (LiteralSchemas, ClassNames);
  111. soapImporter = new SoapSchemaImporter (EncodedSchemas, ClassNames);
  112. xmlExporter = new XmlCodeExporter (CodeNamespace, null);
  113. soapExporter = new SoapCodeExporter (CodeNamespace, null);
  114. }
  115. protected override void EndClass ()
  116. {
  117. SoapTransportImporter transportImporter = SoapTransportImporter.FindTransportImporter (soapBinding.Transport);
  118. if (transportImporter == null) throw new InvalidOperationException ("Transport '" + soapBinding.Transport + "' not supported");
  119. transportImporter.ImportContext = this;
  120. transportImporter.ImportClass ();
  121. if (xmlExporter.IncludeMetadata.Count > 0 || soapExporter.IncludeMetadata.Count > 0)
  122. {
  123. if (CodeTypeDeclaration.CustomAttributes == null)
  124. CodeTypeDeclaration.CustomAttributes = new CodeAttributeDeclarationCollection ();
  125. CodeTypeDeclaration.CustomAttributes.AddRange (xmlExporter.IncludeMetadata);
  126. CodeTypeDeclaration.CustomAttributes.AddRange (soapExporter.IncludeMetadata);
  127. }
  128. }
  129. protected override void EndNamespace ()
  130. {
  131. }
  132. protected override bool IsBindingSupported ()
  133. {
  134. return Binding.Extensions.Find (typeof(SoapBinding)) != null;
  135. }
  136. [MonoTODO]
  137. protected override bool IsOperationFlowSupported (OperationFlow flow)
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. [MonoTODO]
  142. protected virtual bool IsSoapEncodingPresent (string uriList)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. protected override CodeMemberMethod GenerateMethod ()
  147. {
  148. try
  149. {
  150. SoapOperationBinding soapOper = OperationBinding.Extensions.Find (typeof (SoapOperationBinding)) as SoapOperationBinding;
  151. if (soapOper == null) throw new InvalidOperationException ("Soap operation binding not found");
  152. SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
  153. SoapBodyBinding isbb = null;
  154. XmlMembersMapping inputMembers = null;
  155. isbb = OperationBinding.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
  156. if (isbb == null) throw new InvalidOperationException ("Soap body binding not found");
  157. inputMembers = ImportMembersMapping (InputMessage, isbb, style, false);
  158. if (inputMembers == null) throw new InvalidOperationException ("Input message not declared");
  159. // If OperationBinding.Output is null, it is an OneWay operation
  160. SoapBodyBinding osbb = null;
  161. XmlMembersMapping outputMembers = null;
  162. if (OperationBinding.Output != null) {
  163. osbb = OperationBinding.Output.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
  164. if (osbb == null) throw new InvalidOperationException ("Soap body binding not found");
  165. outputMembers = ImportMembersMapping (OutputMessage, osbb, style, true);
  166. if (outputMembers == null) throw new InvalidOperationException ("Output message not declared");
  167. }
  168. CodeMemberMethod met = GenerateMethod (memberIds, soapOper, isbb, inputMembers, outputMembers);
  169. if (isbb.Use == SoapBindingUse.Literal)
  170. xmlExporter.ExportMembersMapping (inputMembers);
  171. else
  172. soapExporter.ExportMembersMapping (inputMembers);
  173. if (osbb != null) {
  174. if (osbb.Use == SoapBindingUse.Literal)
  175. xmlExporter.ExportMembersMapping (outputMembers);
  176. else
  177. soapExporter.ExportMembersMapping (outputMembers);
  178. }
  179. foreach (SoapExtensionImporter eximporter in extensionImporters)
  180. {
  181. eximporter.ImportContext = this;
  182. eximporter.ImportMethod (met.CustomAttributes);
  183. }
  184. return met;
  185. }
  186. catch (InvalidOperationException ex)
  187. {
  188. UnsupportedOperationBindingWarning (ex.Message);
  189. return null;
  190. }
  191. }
  192. XmlMembersMapping ImportMembersMapping (Message msg, SoapBodyBinding sbb, SoapBindingStyle style, bool output)
  193. {
  194. string elemName = Operation.Name;
  195. if (output) elemName += "Response";
  196. if (msg.Parts.Count == 1 && msg.Parts[0].Name == "parameters")
  197. {
  198. // Wrapped parameter style
  199. MessagePart part = msg.Parts[0];
  200. if (sbb.Use == SoapBindingUse.Encoded)
  201. {
  202. SoapSchemaMember ssm = new SoapSchemaMember ();
  203. ssm.MemberName = part.Name;
  204. ssm.MemberType = part.Type;
  205. return soapImporter.ImportMembersMapping (elemName, part.Type.Namespace, ssm);
  206. }
  207. else
  208. return xmlImporter.ImportMembersMapping (part.Element);
  209. }
  210. else
  211. {
  212. if (sbb.Use == SoapBindingUse.Encoded)
  213. {
  214. SoapSchemaMember[] mems = new SoapSchemaMember [msg.Parts.Count];
  215. for (int n=0; n<mems.Length; n++)
  216. {
  217. SoapSchemaMember mem = new SoapSchemaMember();
  218. mem.MemberName = msg.Parts[n].Name;
  219. mem.MemberType = msg.Parts[n].Type;
  220. mems[n] = mem;
  221. }
  222. // Rpc messages always have a wrapping element
  223. if (style == SoapBindingStyle.Rpc)
  224. return soapImporter.ImportMembersMapping (elemName, sbb.Namespace, mems, true);
  225. else
  226. return soapImporter.ImportMembersMapping ("", "", mems, false);
  227. }
  228. else
  229. {
  230. if (style == SoapBindingStyle.Rpc)
  231. throw new InvalidOperationException ("The combination of style=rpc with use=literal is not supported");
  232. if (msg.Parts.Count == 1 && msg.Parts[0].Type != XmlQualifiedName.Empty)
  233. return xmlImporter.ImportAnyType (msg.Parts[0].Type, null);
  234. else
  235. {
  236. XmlQualifiedName[] pnames = new XmlQualifiedName [msg.Parts.Count];
  237. for (int n=0; n<pnames.Length; n++)
  238. pnames[n] = msg.Parts[n].Element;
  239. return xmlImporter.ImportMembersMapping (pnames);
  240. }
  241. }
  242. }
  243. }
  244. CodeMemberMethod GenerateMethod (CodeIdentifiers memberIds, SoapOperationBinding soapOper, SoapBodyBinding bodyBinding, XmlMembersMapping inputMembers, XmlMembersMapping outputMembers)
  245. {
  246. CodeIdentifiers pids = new CodeIdentifiers ();
  247. CodeMemberMethod method = new CodeMemberMethod ();
  248. CodeMemberMethod methodBegin = new CodeMemberMethod ();
  249. CodeMemberMethod methodEnd = new CodeMemberMethod ();
  250. method.Attributes = MemberAttributes.Public;
  251. methodBegin.Attributes = MemberAttributes.Public;
  252. methodEnd.Attributes = MemberAttributes.Public;
  253. SoapBindingStyle style = soapOper.Style != SoapBindingStyle.Default ? soapOper.Style : soapBinding.Style;
  254. // Find unique names for temporary variables
  255. for (int n=0; n<inputMembers.Count; n++)
  256. pids.AddUnique (inputMembers[n].MemberName, inputMembers[n]);
  257. if (outputMembers != null)
  258. for (int n=0; n<outputMembers.Count; n++)
  259. pids.AddUnique (outputMembers[n].MemberName, outputMembers[n]);
  260. string varAsyncResult = pids.AddUnique ("asyncResult","asyncResult");
  261. string varResults = pids.AddUnique ("results","results");
  262. string varCallback = pids.AddUnique ("callback","callback");
  263. string varAsyncState = pids.AddUnique ("asyncState","asyncState");
  264. string messageName = memberIds.AddUnique(CodeIdentifier.MakeValid(Operation.Name),method);
  265. method.Name = CodeIdentifier.MakeValid(Operation.Name);
  266. if (method.Name == ClassName) method.Name += "1";
  267. methodBegin.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("Begin" + method.Name),method);
  268. methodEnd.Name = memberIds.AddUnique(CodeIdentifier.MakeValid("End" + method.Name),method);
  269. method.ReturnType = new CodeTypeReference (typeof(void));
  270. methodEnd.ReturnType = new CodeTypeReference (typeof(void));
  271. methodEnd.Parameters.Add (new CodeParameterDeclarationExpression (typeof (IAsyncResult),varAsyncResult));
  272. CodeExpression[] paramArray = new CodeExpression [inputMembers.Count];
  273. CodeParameterDeclarationExpression[] outParams = new CodeParameterDeclarationExpression [outputMembers != null ? outputMembers.Count : 0];
  274. for (int n=0; n<inputMembers.Count; n++)
  275. {
  276. CodeParameterDeclarationExpression param = GenerateParameter (inputMembers[n], FieldDirection.In);
  277. method.Parameters.Add (param);
  278. GenerateMemberAttributes (inputMembers, inputMembers[n], bodyBinding.Use, param);
  279. methodBegin.Parameters.Add (GenerateParameter (inputMembers[n], FieldDirection.In));
  280. paramArray [n] = new CodeVariableReferenceExpression (param.Name);
  281. }
  282. if (outputMembers != null)
  283. {
  284. bool hasReturn = false;
  285. for (int n=0; n<outputMembers.Count; n++)
  286. {
  287. CodeParameterDeclarationExpression cpd = GenerateParameter (outputMembers[n], FieldDirection.Out);
  288. outParams [n] = cpd;
  289. bool found = false;
  290. foreach (CodeParameterDeclarationExpression ip in method.Parameters)
  291. {
  292. if (ip.Name == cpd.Name && ip.Type.BaseType == cpd.Type.BaseType) {
  293. ip.Direction = FieldDirection.Ref;
  294. methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
  295. found = true;
  296. break;
  297. }
  298. }
  299. if (found) continue;
  300. if (!hasReturn)
  301. {
  302. hasReturn = true;
  303. method.ReturnType = cpd.Type;
  304. methodEnd.ReturnType = cpd.Type;
  305. GenerateReturnAttributes (outputMembers, outputMembers[n], bodyBinding.Use, method);
  306. outParams [n] = null;
  307. continue;
  308. }
  309. method.Parameters.Add (cpd);
  310. GenerateMemberAttributes (outputMembers, outputMembers[n], bodyBinding.Use, cpd);
  311. methodEnd.Parameters.Add (GenerateParameter (outputMembers[n], FieldDirection.Out));
  312. }
  313. }
  314. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (AsyncCallback),varCallback));
  315. methodBegin.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object),varAsyncState));
  316. methodBegin.ReturnType = new CodeTypeReference (typeof(IAsyncResult));
  317. // Array of input parameters
  318. CodeArrayCreateExpression methodParams;
  319. if (paramArray.Length > 0)
  320. methodParams = new CodeArrayCreateExpression (typeof(object), paramArray);
  321. else
  322. methodParams = new CodeArrayCreateExpression (typeof(object), 0);
  323. // Assignment of output parameters
  324. CodeStatementCollection outAssign = new CodeStatementCollection ();
  325. CodeVariableReferenceExpression arrVar = new CodeVariableReferenceExpression (varResults);
  326. for (int n=0; n<outParams.Length; n++)
  327. {
  328. CodeExpression index = new CodePrimitiveExpression (n);
  329. if (outParams[n] == null)
  330. {
  331. CodeExpression res = new CodeCastExpression (method.ReturnType, new CodeArrayIndexerExpression (arrVar, index));
  332. outAssign.Add (new CodeMethodReturnStatement (res));
  333. }
  334. else
  335. {
  336. CodeExpression res = new CodeCastExpression (outParams[n].Type, new CodeArrayIndexerExpression (arrVar, index));
  337. CodeExpression var = new CodeVariableReferenceExpression (outParams[n].Name);
  338. outAssign.Insert (0, new CodeAssignStatement (var, res));
  339. }
  340. }
  341. // Invoke call
  342. CodeThisReferenceExpression ethis = new CodeThisReferenceExpression();
  343. CodePrimitiveExpression varMsgName = new CodePrimitiveExpression (messageName);
  344. CodeMethodInvokeExpression inv;
  345. CodeVariableDeclarationStatement dec;
  346. inv = new CodeMethodInvokeExpression (ethis, "Invoke", varMsgName, methodParams);
  347. if (outputMembers != null && outputMembers.Count > 0)
  348. {
  349. dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
  350. method.Statements.Add (dec);
  351. method.Statements.AddRange (outAssign);
  352. }
  353. else
  354. method.Statements.Add (inv);
  355. // Begin Invoke Call
  356. CodeExpression expCallb = new CodeVariableReferenceExpression (varCallback);
  357. CodeExpression expAsyncs = new CodeVariableReferenceExpression (varAsyncState);
  358. inv = new CodeMethodInvokeExpression (ethis, "BeginInvoke", varMsgName, methodParams, expCallb, expAsyncs);
  359. methodBegin.Statements.Add (new CodeMethodReturnStatement (inv));
  360. // End Invoke call
  361. CodeExpression varAsyncr = new CodeVariableReferenceExpression (varAsyncResult);
  362. inv = new CodeMethodInvokeExpression (ethis, "EndInvoke", varAsyncr);
  363. if (outputMembers != null && outputMembers.Count > 0)
  364. {
  365. dec = new CodeVariableDeclarationStatement (typeof(object[]), varResults, inv);
  366. methodEnd.Statements.Add (dec);
  367. methodEnd.Statements.AddRange (outAssign);
  368. }
  369. else
  370. methodEnd.Statements.Add (inv);
  371. // Attributes
  372. ImportHeaders (method);
  373. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.WebMethodAttribute");
  374. if (messageName != method.Name) att.Arguments.Add (GetArg ("MessageName",messageName));
  375. AddCustomAttribute (method, att, false);
  376. if (style == SoapBindingStyle.Rpc)
  377. {
  378. att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapRpcMethodAttribute");
  379. att.Arguments.Add (GetArg (soapOper.SoapAction));
  380. if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
  381. if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
  382. att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
  383. if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
  384. if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
  385. }
  386. else
  387. {
  388. if (outputMembers != null && (inputMembers.ElementName == "" && outputMembers.ElementName != "" ||
  389. inputMembers.ElementName != "" && outputMembers.ElementName == ""))
  390. throw new InvalidOperationException ("Parameter style is not the same for the input message and output message");
  391. att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapDocumentMethodAttribute");
  392. att.Arguments.Add (GetArg (soapOper.SoapAction));
  393. if (inputMembers.ElementName != "") {
  394. if (inputMembers.ElementName != method.Name) att.Arguments.Add (GetArg ("RequestElementName", inputMembers.ElementName));
  395. if (outputMembers != null && outputMembers.ElementName != (method.Name + "Response")) att.Arguments.Add (GetArg ("ResponseElementName", outputMembers.ElementName));
  396. att.Arguments.Add (GetArg ("RequestNamespace", inputMembers.Namespace));
  397. if (outputMembers != null) att.Arguments.Add (GetArg ("ResponseNamespace", outputMembers.Namespace));
  398. att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Wrapped"));
  399. }
  400. else
  401. att.Arguments.Add (GetEnumArg ("ParameterStyle", "System.Web.Services.Protocols.SoapParameterStyle", "Bare"));
  402. if (outputMembers == null) att.Arguments.Add (GetArg ("OneWay", true));
  403. att.Arguments.Add (GetEnumArg ("Use", "System.Web.Services.Description.SoapBindingUse", bodyBinding.Use.ToString()));
  404. }
  405. AddCustomAttribute (method, att, true);
  406. CodeTypeDeclaration.Members.Add (method);
  407. CodeTypeDeclaration.Members.Add (methodBegin);
  408. CodeTypeDeclaration.Members.Add (methodEnd);
  409. return method;
  410. }
  411. CodeParameterDeclarationExpression GenerateParameter (XmlMemberMapping member, FieldDirection dir)
  412. {
  413. CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression (member.TypeFullName, member.MemberName);
  414. par.Direction = dir;
  415. return par;
  416. }
  417. void GenerateMemberAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeParameterDeclarationExpression param)
  418. {
  419. if (use == SoapBindingUse.Literal)
  420. xmlExporter.AddMappingMetadata (param.CustomAttributes, member, members.Namespace);
  421. else
  422. soapExporter.AddMappingMetadata (param.CustomAttributes, member);
  423. }
  424. void GenerateReturnAttributes (XmlMembersMapping members, XmlMemberMapping member, SoapBindingUse use, CodeMemberMethod method)
  425. {
  426. if (use == SoapBindingUse.Literal)
  427. xmlExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, members.Namespace, (member.ElementName != method.Name + "Result"));
  428. else
  429. soapExporter.AddMappingMetadata (method.ReturnTypeCustomAttributes, member, (member.ElementName != method.Name + "Result"));
  430. }
  431. void ImportHeaders (CodeMemberMethod method)
  432. {
  433. foreach (object ob in OperationBinding.Input.Extensions)
  434. {
  435. SoapHeaderBinding hb = ob as SoapHeaderBinding;
  436. if (hb == null) continue;
  437. if (HasHeader (OperationBinding.Output, hb))
  438. ImportHeader (method, hb, SoapHeaderDirection.In | SoapHeaderDirection.Out);
  439. else
  440. ImportHeader (method, hb, SoapHeaderDirection.In);
  441. }
  442. if (OperationBinding.Output == null) return;
  443. foreach (object ob in OperationBinding.Output.Extensions)
  444. {
  445. SoapHeaderBinding hb = ob as SoapHeaderBinding;
  446. if (hb == null) continue;
  447. if (!HasHeader (OperationBinding.Input, hb))
  448. ImportHeader (method, hb, SoapHeaderDirection.Out);
  449. }
  450. }
  451. bool HasHeader (MessageBinding msg, SoapHeaderBinding hb)
  452. {
  453. if (msg == null) return false;
  454. foreach (object ob in msg.Extensions)
  455. {
  456. SoapHeaderBinding mhb = ob as SoapHeaderBinding;
  457. if ((mhb != null) && (mhb.Message == hb.Message) && (mhb.Part == hb.Part))
  458. return true;
  459. }
  460. return false;
  461. }
  462. void ImportHeader (CodeMemberMethod method, SoapHeaderBinding hb, SoapHeaderDirection direction)
  463. {
  464. Message msg = ServiceDescriptions.GetMessage (hb.Message);
  465. if (msg == null) throw new InvalidOperationException ("Message " + hb.Message + " not found");
  466. MessagePart part = msg.Parts [hb.Part];
  467. if (part == null) throw new InvalidOperationException ("Message part " + hb.Part + " not found in message " + hb.Message);
  468. XmlTypeMapping map;
  469. if (hb.Use == SoapBindingUse.Literal)
  470. {
  471. map = xmlImporter.ImportDerivedTypeMapping (part.Element, typeof (SoapHeader));
  472. xmlExporter.ExportTypeMapping (map);
  473. }
  474. else
  475. {
  476. map = soapImporter.ImportDerivedTypeMapping (part.Type, typeof (SoapHeader), true);
  477. soapExporter.ExportTypeMapping (map);
  478. }
  479. bool required = false;
  480. string varName = headerVariables [map] as string;
  481. if (varName == null)
  482. {
  483. varName = memberIds.AddUnique(CodeIdentifier.MakeValid (map.TypeName + "Value"),hb);
  484. headerVariables.Add (map, varName);
  485. CodeMemberField codeField = new CodeMemberField (map.TypeFullName, varName);
  486. codeField.Attributes = MemberAttributes.Public;
  487. CodeTypeDeclaration.Members.Add (codeField);
  488. }
  489. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Web.Services.Protocols.SoapHeaderAttribute");
  490. att.Arguments.Add (GetArg (varName));
  491. att.Arguments.Add (GetArg ("Required", required));
  492. if (direction != SoapHeaderDirection.In) att.Arguments.Add (GetEnumArg ("Direction", "System.Web.Services.Protocols.SoapHeaderDirection", direction.ToString ()));
  493. AddCustomAttribute (method, att, true);
  494. }
  495. #endregion
  496. }
  497. }