PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ServiceModel/System.ServiceModel.Description/ServiceContractGenerator.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 887 lines | 679 code | 140 blank | 68 comment | 94 complexity | 7a1c9c2df5e2b78878adef124f35c78c MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // ServiceContractGenerator.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.CodeDom;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.ComponentModel;
  33. using System.Configuration;
  34. using System.Linq;
  35. using System.Reflection;
  36. using System.Runtime.Serialization;
  37. using System.ServiceModel;
  38. using System.ServiceModel.Channels;
  39. using System.ServiceModel.Configuration;
  40. using System.Threading;
  41. using System.Xml.Schema;
  42. using System.Xml.Serialization;
  43. using ConfigurationType = System.Configuration.Configuration;
  44. using QName = System.Xml.XmlQualifiedName;
  45. using OPair = System.Collections.Generic.KeyValuePair<System.ServiceModel.Description.IOperationContractGenerationExtension,System.ServiceModel.Description.OperationContractGenerationContext>;
  46. namespace System.ServiceModel.Description
  47. {
  48. public class ServiceContractGenerator
  49. {
  50. CodeCompileUnit ccu;
  51. ConfigurationType config;
  52. Collection<MetadataConversionError> errors
  53. = new Collection<MetadataConversionError> ();
  54. Dictionary<string,string> nsmappings
  55. = new Dictionary<string,string> ();
  56. Dictionary<ContractDescription,Type> referenced_types
  57. = new Dictionary<ContractDescription,Type> ();
  58. ServiceContractGenerationOptions options;
  59. Dictionary<QName, QName> imported_names = null;
  60. ServiceContractGenerationContext contract_context;
  61. List<OPair> operation_contexts = new List<OPair> ();
  62. public ServiceContractGenerator ()
  63. : this (null, null)
  64. {
  65. }
  66. public ServiceContractGenerator (CodeCompileUnit ccu)
  67. : this (ccu, null)
  68. {
  69. }
  70. public ServiceContractGenerator (ConfigurationType config)
  71. : this (null, config)
  72. {
  73. }
  74. public ServiceContractGenerator (CodeCompileUnit ccu, ConfigurationType config)
  75. {
  76. if (ccu == null)
  77. this.ccu = new CodeCompileUnit ();
  78. else
  79. this.ccu = ccu;
  80. this.config = config;
  81. Options |= ServiceContractGenerationOptions.ChannelInterface |
  82. ServiceContractGenerationOptions.ClientClass;
  83. }
  84. public ConfigurationType Configuration {
  85. get { return config; }
  86. }
  87. public Collection<MetadataConversionError> Errors {
  88. get { return errors; }
  89. }
  90. public Dictionary<string,string> NamespaceMappings {
  91. get { return nsmappings; }
  92. }
  93. public ServiceContractGenerationOptions Options {
  94. get { return options; }
  95. set { options = value; }
  96. }
  97. bool GenerateAsync {
  98. get { return GenerateEventBasedAsync || (options & ServiceContractGenerationOptions.AsynchronousMethods) != 0; }
  99. }
  100. bool GenerateEventBasedAsync {
  101. get { return (options & ServiceContractGenerationOptions.EventBasedAsynchronousMethods) != 0; }
  102. }
  103. public Dictionary<ContractDescription,Type> ReferencedTypes {
  104. get { return referenced_types; }
  105. }
  106. public CodeCompileUnit TargetCompileUnit {
  107. get { return ccu; }
  108. }
  109. [MonoTODO]
  110. public void GenerateBinding (Binding binding,
  111. out string bindingSectionName,
  112. out string configurationName)
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. #region Service Contract Type
  117. // Those implementation classes are very likely to be split
  118. // into different classes.
  119. [MonoTODO]
  120. public CodeTypeReference GenerateServiceContractType (
  121. ContractDescription contractDescription)
  122. {
  123. CodeNamespace cns = GetNamespace (contractDescription.Namespace);
  124. imported_names = new Dictionary<QName, QName> ();
  125. var ret = ExportInterface (contractDescription, cns);
  126. // FIXME: handle duplex callback
  127. if ((Options & ServiceContractGenerationOptions.ChannelInterface) != 0)
  128. GenerateChannelInterface (contractDescription, cns);
  129. if ((Options & ServiceContractGenerationOptions.ClientClass) != 0)
  130. GenerateProxyClass (contractDescription, cns);
  131. // Process extensions. Class first, then methods.
  132. // (built-in ones must present before processing class extensions).
  133. foreach (var cb in contractDescription.Behaviors) {
  134. var gex = cb as IServiceContractGenerationExtension;
  135. if (gex != null)
  136. gex.GenerateContract (contract_context);
  137. }
  138. foreach (var opair in operation_contexts)
  139. opair.Key.GenerateOperation (opair.Value);
  140. return ret;
  141. }
  142. CodeNamespace GetNamespace (string ns)
  143. {
  144. if (ns == null)
  145. ns = String.Empty;
  146. foreach (CodeNamespace cns in ccu.Namespaces)
  147. if (cns.Name == ns)
  148. return cns;
  149. CodeNamespace ncns = new CodeNamespace ();
  150. //ncns.Name = ns;
  151. ccu.Namespaces.Add (ncns);
  152. return ncns;
  153. }
  154. CodeTypeDeclaration GetTypeDeclaration (CodeNamespace cns, string name)
  155. {
  156. foreach (CodeTypeDeclaration type in cns.Types)
  157. if (type.Name == name)
  158. return type;
  159. return null;
  160. }
  161. void GenerateProxyClass (ContractDescription cd, CodeNamespace cns)
  162. {
  163. string name = cd.Name + "Client";
  164. if (name [0] == 'I')
  165. name = name.Substring (1);
  166. CodeTypeDeclaration type = GetTypeDeclaration (cns, name);
  167. if (type != null)
  168. return; // already imported
  169. CodeTypeReference clientBase = new CodeTypeReference (typeof (ClientBase<>));
  170. clientBase.TypeArguments.Add (new CodeTypeReference (cd.Name));
  171. type = new CodeTypeDeclaration (name);
  172. cns.Types.Add (type);
  173. type.TypeAttributes = TypeAttributes.Public;
  174. type.BaseTypes.Add (clientBase);
  175. type.BaseTypes.Add (new CodeTypeReference (cd.Name));
  176. // .ctor()
  177. CodeConstructor ctor = new CodeConstructor ();
  178. ctor.Attributes = MemberAttributes.Public;
  179. type.Members.Add (ctor);
  180. // .ctor(string endpointConfigurationName)
  181. ctor = new CodeConstructor ();
  182. ctor.Attributes = MemberAttributes.Public;
  183. ctor.Parameters.Add (
  184. new CodeParameterDeclarationExpression (
  185. new CodeTypeReference (typeof (string)), "endpointConfigurationName"));
  186. ctor.BaseConstructorArgs.Add (
  187. new CodeArgumentReferenceExpression ("endpointConfigurationName"));
  188. type.Members.Add (ctor);
  189. // .ctor(string endpointConfigurationName, string remoteAddress)
  190. ctor = new CodeConstructor ();
  191. ctor.Attributes = MemberAttributes.Public;
  192. ctor.Parameters.Add (
  193. new CodeParameterDeclarationExpression (
  194. new CodeTypeReference (typeof (string)), "endpointConfigurationName"));
  195. ctor.Parameters.Add (
  196. new CodeParameterDeclarationExpression (
  197. new CodeTypeReference (typeof (string)), "remoteAddress"));
  198. ctor.BaseConstructorArgs.Add (
  199. new CodeArgumentReferenceExpression ("endpointConfigurationName"));
  200. ctor.BaseConstructorArgs.Add (
  201. new CodeArgumentReferenceExpression ("remoteAddress"));
  202. type.Members.Add (ctor);
  203. // .ctor(string endpointConfigurationName, EndpointAddress remoteAddress)
  204. ctor = new CodeConstructor ();
  205. ctor.Attributes = MemberAttributes.Public;
  206. ctor.Parameters.Add (
  207. new CodeParameterDeclarationExpression (
  208. new CodeTypeReference (typeof (string)), "endpointConfigurationName"));
  209. ctor.Parameters.Add (
  210. new CodeParameterDeclarationExpression (
  211. new CodeTypeReference (typeof (EndpointAddress)), "remoteAddress"));
  212. ctor.BaseConstructorArgs.Add (
  213. new CodeArgumentReferenceExpression ("endpointConfigurationName"));
  214. ctor.BaseConstructorArgs.Add (
  215. new CodeArgumentReferenceExpression ("remoteAddress"));
  216. type.Members.Add (ctor);
  217. // .ctor(Binding,EndpointAddress)
  218. ctor = new CodeConstructor ();
  219. ctor.Attributes = MemberAttributes.Public;
  220. ctor.Parameters.Add (
  221. new CodeParameterDeclarationExpression (
  222. new CodeTypeReference (typeof (Binding)), "binding"));
  223. ctor.Parameters.Add (
  224. new CodeParameterDeclarationExpression (
  225. new CodeTypeReference (typeof (EndpointAddress)), "endpoint"));
  226. ctor.BaseConstructorArgs.Add (
  227. new CodeArgumentReferenceExpression ("binding"));
  228. ctor.BaseConstructorArgs.Add (
  229. new CodeArgumentReferenceExpression ("endpoint"));
  230. type.Members.Add (ctor);
  231. // service contract methods
  232. AddImplementationClientMethods (type, cd);
  233. if (GenerateEventBasedAsync)
  234. foreach (var od in cd.Operations)
  235. GenerateEventBasedAsyncSupport (type, od, cns);
  236. }
  237. void GenerateChannelInterface (ContractDescription cd, CodeNamespace cns)
  238. {
  239. string name = cd.Name + "Channel";
  240. CodeTypeDeclaration type = GetTypeDeclaration (cns, name);
  241. if (type != null)
  242. return;
  243. type = new CodeTypeDeclaration ();
  244. type.Name = name;
  245. type.TypeAttributes = TypeAttributes.Interface | TypeAttributes.Public;
  246. cns.Types.Add (type);
  247. type.BaseTypes.Add (ExportInterface (cd, cns));
  248. type.BaseTypes.Add (new CodeTypeReference (typeof (System.ServiceModel.IClientChannel)));
  249. }
  250. CodeTypeReference ExportInterface (ContractDescription cd, CodeNamespace cns)
  251. {
  252. CodeTypeDeclaration type = GetTypeDeclaration (cns, cd.Name);
  253. if (type != null)
  254. return new CodeTypeReference (type.Name);
  255. type = new CodeTypeDeclaration ();
  256. type.TypeAttributes = TypeAttributes.Interface;
  257. type.TypeAttributes |= TypeAttributes.Public;
  258. cns.Types.Add (type);
  259. type.Name = cd.Name;
  260. CodeAttributeDeclaration ad =
  261. new CodeAttributeDeclaration (
  262. new CodeTypeReference (
  263. typeof (ServiceContractAttribute)));
  264. ad.Arguments.Add (new CodeAttributeArgument ("Namespace", new CodePrimitiveExpression (cd.Namespace)));
  265. type.CustomAttributes.Add (ad);
  266. contract_context = new ServiceContractGenerationContext (this, cd, type);
  267. AddOperationMethods (type, cd);
  268. return new CodeTypeReference (type.Name);
  269. }
  270. void AddBeginAsyncArgs (CodeMemberMethod cm)
  271. {
  272. var acb = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (AsyncCallback)), "asyncCallback");
  273. cm.Parameters.Add (acb);
  274. var us = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (object)), "userState");
  275. cm.Parameters.Add (us);
  276. }
  277. void AddOperationMethods (CodeTypeDeclaration type, ContractDescription cd)
  278. {
  279. foreach (OperationDescription od in cd.Operations) {
  280. CodeMemberMethod syncMethod = null, beginMethod = null, endMethod = null;
  281. CodeTypeReference returnTypeFromMessageContract = null;
  282. syncMethod = GenerateOperationMethod (type, cd, od, false, out returnTypeFromMessageContract);
  283. type.Members.Add (syncMethod);
  284. if (GenerateAsync) {
  285. beginMethod = GenerateOperationMethod (type, cd, od, true, out returnTypeFromMessageContract);
  286. type.Members.Add (beginMethod);
  287. var cm = new CodeMemberMethod ();
  288. type.Members.Add (cm);
  289. cm.Name = "End" + od.Name;
  290. endMethod = cm;
  291. var res = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (IAsyncResult)), "result");
  292. cm.Parameters.Add (res);
  293. if (od.SyncMethod != null) // FIXME: it depends on sync method!
  294. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  295. else
  296. cm.ReturnType = returnTypeFromMessageContract;
  297. }
  298. foreach (var ob in od.Behaviors) {
  299. var gex = ob as IOperationContractGenerationExtension;
  300. if (gex != null)
  301. operation_contexts.Add (new OPair (gex, new OperationContractGenerationContext (this, contract_context, od, type, syncMethod, beginMethod, endMethod)));
  302. }
  303. }
  304. }
  305. CodeMemberMethod GenerateOperationMethod (CodeTypeDeclaration type, ContractDescription cd, OperationDescription od, bool async, out CodeTypeReference returnType)
  306. {
  307. CodeMemberMethod cm = new CodeMemberMethod ();
  308. if (async)
  309. cm.Name = "Begin" + od.Name;
  310. else
  311. cm.Name = od.Name;
  312. if (od.SyncMethod != null) {
  313. ExportParameters (cm, od.SyncMethod.GetParameters ());
  314. if (async) {
  315. AddBeginAsyncArgs (cm);
  316. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  317. }
  318. else
  319. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  320. returnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  321. } else {
  322. ExportMessages (od.Messages, cm, false);
  323. returnType = cm.ReturnType;
  324. if (async) {
  325. AddBeginAsyncArgs (cm);
  326. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  327. }
  328. }
  329. // [OperationContract (Action = "...", ReplyAction = "..")]
  330. var ad = new CodeAttributeDeclaration (new CodeTypeReference (typeof (OperationContractAttribute)));
  331. foreach (MessageDescription md in od.Messages) {
  332. if (md.Direction == MessageDirection.Input)
  333. ad.Arguments.Add (new CodeAttributeArgument ("Action", new CodePrimitiveExpression (md.Action)));
  334. else
  335. ad.Arguments.Add (new CodeAttributeArgument ("ReplyAction", new CodePrimitiveExpression (md.Action)));
  336. }
  337. if (async)
  338. ad.Arguments.Add (new CodeAttributeArgument ("AsyncPattern", new CodePrimitiveExpression (true)));
  339. cm.CustomAttributes.Add (ad);
  340. return cm;
  341. }
  342. void ExportParameters (CodeMemberMethod method, ParameterInfo [] parameters)
  343. {
  344. foreach (ParameterInfo pi in parameters)
  345. method.Parameters.Add (
  346. new CodeParameterDeclarationExpression (
  347. new CodeTypeReference (pi.ParameterType),
  348. pi.Name));
  349. }
  350. void AddImplementationClientMethods (CodeTypeDeclaration type, ContractDescription cd)
  351. {
  352. foreach (OperationDescription od in cd.Operations) {
  353. CodeMemberMethod cm;
  354. CodeTypeReference returnTypeFromMessageContract = null;
  355. cm = GenerateImplementationClientMethod (type, cd, od, false, out returnTypeFromMessageContract);
  356. type.Members.Add (cm);
  357. if (!GenerateAsync)
  358. continue;
  359. cm = GenerateImplementationClientMethod (type, cd, od, true, out returnTypeFromMessageContract);
  360. type.Members.Add (cm);
  361. // EndXxx() implementation
  362. cm = new CodeMemberMethod ();
  363. cm.Attributes = MemberAttributes.Public
  364. | MemberAttributes.Final;
  365. type.Members.Add (cm);
  366. cm.Name = "End" + od.Name;
  367. var res = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (IAsyncResult)), "result");
  368. cm.Parameters.Add (res);
  369. if (od.SyncMethod != null) // FIXME: it depends on sync method!
  370. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  371. else
  372. cm.ReturnType = returnTypeFromMessageContract;
  373. string resultArgName = "result";
  374. if (od.EndMethod != null)
  375. resultArgName = od.EndMethod.GetParameters () [0].Name;
  376. var call = new CodeMethodInvokeExpression (
  377. new CodePropertyReferenceExpression (
  378. new CodeBaseReferenceExpression (),
  379. "Channel"),
  380. cm.Name,
  381. new CodeArgumentReferenceExpression (resultArgName));
  382. if (cm.ReturnType.BaseType == "System.Void")
  383. cm.Statements.Add (new CodeExpressionStatement (call));
  384. else
  385. cm.Statements.Add (new CodeMethodReturnStatement (call));
  386. }
  387. }
  388. CodeMemberMethod GenerateImplementationClientMethod (CodeTypeDeclaration type, ContractDescription cd, OperationDescription od, bool async, out CodeTypeReference returnTypeFromMessageContract)
  389. {
  390. CodeMemberMethod cm = new CodeMemberMethod ();
  391. if (async)
  392. cm.Name = "Begin" + od.Name;
  393. else
  394. cm.Name = od.Name;
  395. cm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  396. returnTypeFromMessageContract = null;
  397. List<CodeExpression> args = new List<CodeExpression> ();
  398. if (od.SyncMethod != null) {
  399. ParameterInfo [] pars = od.SyncMethod.GetParameters ();
  400. ExportParameters (cm, pars);
  401. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  402. int i = 0;
  403. foreach (ParameterInfo pi in pars)
  404. args.Add (new CodeArgumentReferenceExpression (pi.Name));
  405. } else {
  406. args.AddRange (ExportMessages (od.Messages, cm, true));
  407. returnTypeFromMessageContract = cm.ReturnType;
  408. if (async) {
  409. AddBeginAsyncArgs (cm);
  410. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  411. }
  412. }
  413. if (async) {
  414. args.Add (new CodeArgumentReferenceExpression ("asyncCallback"));
  415. args.Add (new CodeArgumentReferenceExpression ("userState"));
  416. }
  417. CodeExpression call = new CodeMethodInvokeExpression (
  418. new CodePropertyReferenceExpression (
  419. new CodeBaseReferenceExpression (),
  420. "Channel"),
  421. cm.Name,
  422. args.ToArray ());
  423. if (cm.ReturnType.BaseType == "System.Void")
  424. cm.Statements.Add (new CodeExpressionStatement (call));
  425. else
  426. cm.Statements.Add (new CodeMethodReturnStatement (call));
  427. return cm;
  428. }
  429. CodeMemberMethod FindByName (CodeTypeDeclaration type, string name)
  430. {
  431. foreach (var m in type.Members) {
  432. var method = m as CodeMemberMethod;
  433. if (method != null && method.Name == name)
  434. return method;
  435. }
  436. return null;
  437. }
  438. void GenerateEventBasedAsyncSupport (CodeTypeDeclaration type, OperationDescription od, CodeNamespace cns)
  439. {
  440. var method = FindByName (type, od.Name) ?? FindByName (type, "Begin" + od.Name);
  441. var endMethod = method.Name == od.Name ? null : FindByName (type, "End" + od.Name);
  442. bool methodAsync = method.Name.StartsWith ("Begin", StringComparison.Ordinal);
  443. var thisExpr = new CodeThisReferenceExpression ();
  444. var baseExpr = new CodeBaseReferenceExpression ();
  445. var nullExpr = new CodePrimitiveExpression (null);
  446. var asyncResultType = new CodeTypeReference (typeof (IAsyncResult));
  447. // OnBeginXxx() implementation
  448. var cm = new CodeMemberMethod () {
  449. Name = "OnBegin" + od.Name,
  450. Attributes = MemberAttributes.Private | MemberAttributes.Final,
  451. ReturnType = asyncResultType
  452. };
  453. type.Members.Add (cm);
  454. AddMethodParam (cm, typeof (object []), "args");
  455. AddMethodParam (cm, typeof (AsyncCallback), "asyncCallback");
  456. AddMethodParam (cm, typeof (object), "userState");
  457. var call = new CodeMethodInvokeExpression (
  458. thisExpr,
  459. "Begin" + od.Name);
  460. for (int idx = 0; idx < method.Parameters.Count - (methodAsync ? 2 : 0); idx++) {
  461. var p = method.Parameters [idx];
  462. cm.Statements.Add (new CodeVariableDeclarationStatement (p.Type, p.Name, new CodeCastExpression (p.Type, new CodeArrayIndexerExpression (new CodeArgumentReferenceExpression ("args"), new CodePrimitiveExpression (idx)))));
  463. call.Parameters.Add (new CodeVariableReferenceExpression (p.Name));
  464. }
  465. call.Parameters.Add (new CodeArgumentReferenceExpression ("asyncCallback"));
  466. call.Parameters.Add (new CodeArgumentReferenceExpression ("userState"));
  467. cm.Statements.Add (new CodeMethodReturnStatement (call));
  468. // OnEndXxx() implementation
  469. cm = new CodeMemberMethod () {
  470. Name = "OnEnd" + od.Name,
  471. Attributes = MemberAttributes.Private | MemberAttributes.Final,
  472. ReturnType = new CodeTypeReference (typeof (object [])) };
  473. type.Members.Add (cm);
  474. AddMethodParam (cm, typeof (IAsyncResult), "result");
  475. var outArgRefs = new List<CodeVariableReferenceExpression> ();
  476. for (int idx = 0; idx < method.Parameters.Count; idx++) {
  477. var p = method.Parameters [idx];
  478. if (p.Direction != FieldDirection.In) {
  479. cm.Statements.Add (new CodeVariableDeclarationStatement (p.Type, p.Name));
  480. outArgRefs.Add (new CodeVariableReferenceExpression (p.Name)); // FIXME: should this work? They need "out" or "ref" modifiers.
  481. }
  482. }
  483. call = new CodeMethodInvokeExpression (
  484. thisExpr,
  485. "End" + od.Name,
  486. new CodeArgumentReferenceExpression ("result"));
  487. call.Parameters.AddRange (outArgRefs.Cast<CodeExpression> ().ToArray ()); // questionable
  488. cm.Statements.Add (new CodeVariableDeclarationStatement (typeof (object), "__ret", call));
  489. var retCreate = new CodeArrayCreateExpression (typeof (object));
  490. retCreate.Initializers.Add (new CodeVariableReferenceExpression ("__ret"));
  491. foreach (var outArgRef in outArgRefs)
  492. retCreate.Initializers.Add (new CodeVariableReferenceExpression (outArgRef.VariableName));
  493. cm.Statements.Add (new CodeMethodReturnStatement (retCreate));
  494. // OnXxxCompleted() implementation
  495. cm = new CodeMemberMethod () {
  496. Name = "On" + od.Name + "Completed",
  497. Attributes = MemberAttributes.Private | MemberAttributes.Final };
  498. type.Members.Add (cm);
  499. AddMethodParam (cm, typeof (object), "state");
  500. var iaargs = new CodeTypeReference ("InvokeAsyncCompletedEventArgs"); // avoid messy System.Type instance for generic nested type :|
  501. var iaref = new CodeVariableReferenceExpression ("args");
  502. var methodEventArgs = new CodeObjectCreateExpression (new CodeTypeReference (od.Name + "CompletedEventArgs"),
  503. new CodePropertyReferenceExpression (iaref, "Results"),
  504. new CodePropertyReferenceExpression (iaref, "Error"),
  505. new CodePropertyReferenceExpression (iaref, "Cancelled"),
  506. new CodePropertyReferenceExpression (iaref, "UserState"));
  507. cm.Statements.Add (new CodeConditionStatement (
  508. new CodeBinaryOperatorExpression (
  509. new CodeEventReferenceExpression (thisExpr, od.Name + "Completed"), CodeBinaryOperatorType.IdentityInequality, nullExpr),
  510. new CodeVariableDeclarationStatement (iaargs, "args", new CodeCastExpression (iaargs, new CodeArgumentReferenceExpression ("state"))),
  511. new CodeExpressionStatement (new CodeMethodInvokeExpression (thisExpr, od.Name + "Completed", thisExpr, methodEventArgs))));
  512. // delegate fields
  513. type.Members.Add (new CodeMemberField (new CodeTypeReference ("BeginOperationDelegate"), "onBegin" + od.Name + "Delegate"));
  514. type.Members.Add (new CodeMemberField (new CodeTypeReference ("EndOperationDelegate"), "onEnd" + od.Name + "Delegate"));
  515. type.Members.Add (new CodeMemberField (new CodeTypeReference (typeof (SendOrPostCallback)), "on" + od.Name + "CompletedDelegate"));
  516. // XxxCompletedEventArgs class
  517. var argsType = new CodeTypeDeclaration (od.Name + "CompletedEventArgs");
  518. argsType.BaseTypes.Add (new CodeTypeReference (typeof (AsyncCompletedEventArgs)));
  519. cns.Types.Add (argsType);
  520. var argsCtor = new CodeConstructor () {
  521. Attributes = MemberAttributes.Public | MemberAttributes.Final };
  522. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object []), "results"));
  523. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (Exception), "error"));
  524. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (bool), "cancelled"));
  525. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  526. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("error"));
  527. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("cancelled"));
  528. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("userState"));
  529. var resultsField = new CodeFieldReferenceExpression (thisExpr, "results");
  530. argsCtor.Statements.Add (new CodeAssignStatement (resultsField, new CodeArgumentReferenceExpression ("results")));
  531. argsType.Members.Add (argsCtor);
  532. argsType.Members.Add (new CodeMemberField (typeof (object []), "results"));
  533. var resultProp = new CodeMemberProperty {
  534. Name = "Result",
  535. Type = endMethod != null ? endMethod.ReturnType : method.ReturnType,
  536. Attributes = MemberAttributes.Public | MemberAttributes.Final };
  537. resultProp.GetStatements.Add (new CodeMethodReturnStatement (new CodeCastExpression (resultProp.Type, new CodeArrayIndexerExpression (resultsField, new CodePrimitiveExpression (0)))));
  538. argsType.Members.Add (resultProp);
  539. // event field
  540. var handlerType = new CodeTypeReference (typeof (EventHandler<>));
  541. handlerType.TypeArguments.Add (new CodeTypeReference (argsType.Name));
  542. type.Members.Add (new CodeMemberEvent () {
  543. Name = od.Name + "Completed",
  544. Type = handlerType,
  545. Attributes = MemberAttributes.Public | MemberAttributes.Final });
  546. // XxxAsync() implementations
  547. bool hasAsync = false;
  548. foreach (int __x in Enumerable.Range (0, 2)) {
  549. cm = new CodeMemberMethod ();
  550. type.Members.Add (cm);
  551. cm.Name = od.Name + "Async";
  552. cm.Attributes = MemberAttributes.Public
  553. | MemberAttributes.Final;
  554. var inArgs = new List<CodeParameterDeclarationExpression > ();
  555. for (int idx = 0; idx < method.Parameters.Count - (methodAsync ? 2 : 0); idx++) {
  556. var pd = method.Parameters [idx];
  557. inArgs.Add (pd);
  558. cm.Parameters.Add (pd);
  559. }
  560. // First one is overload without asyncState arg.
  561. if (!hasAsync) {
  562. call = new CodeMethodInvokeExpression (thisExpr, cm.Name, inArgs.ConvertAll<CodeExpression> (decl => new CodeArgumentReferenceExpression (decl.Name)).ToArray ());
  563. call.Parameters.Add (nullExpr);
  564. cm.Statements.Add (new CodeExpressionStatement (call));
  565. hasAsync = true;
  566. continue;
  567. }
  568. // Second one is the primary one.
  569. cm.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  570. // if (onBeginBarOperDelegate == null) onBeginBarOperDelegate = new BeginOperationDelegate (OnBeginBarOper);
  571. // if (onEndBarOperDelegate == null) onEndBarOperDelegate = new EndOperationDelegate (OnEndBarOper);
  572. // if (onBarOperCompletedDelegate == null) onBarOperCompletedDelegate = new BeginOperationDelegate (OnBarOperCompleted);
  573. var beginOperDelegateRef = new CodeFieldReferenceExpression (thisExpr, "onBegin" + od.Name + "Delegate");
  574. var endOperDelegateRef = new CodeFieldReferenceExpression (thisExpr, "onEnd" + od.Name + "Delegate");
  575. var operCompletedDelegateRef = new CodeFieldReferenceExpression (thisExpr, "on" + od.Name + "CompletedDelegate");
  576. var ifstmt = new CodeConditionStatement (
  577. new CodeBinaryOperatorExpression (beginOperDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  578. new CodeAssignStatement (beginOperDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference ("BeginOperationDelegate"), thisExpr, "OnBegin" + od.Name)));
  579. cm.Statements.Add (ifstmt);
  580. ifstmt = new CodeConditionStatement (
  581. new CodeBinaryOperatorExpression (endOperDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  582. new CodeAssignStatement (endOperDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference ("EndOperationDelegate"), thisExpr, "OnEnd" + od.Name)));
  583. cm.Statements.Add (ifstmt);
  584. ifstmt = new CodeConditionStatement (
  585. new CodeBinaryOperatorExpression (operCompletedDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  586. new CodeAssignStatement (operCompletedDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference (typeof (SendOrPostCallback)), thisExpr, "On" + od.Name + "Completed")));
  587. cm.Statements.Add (ifstmt);
  588. // InvokeAsync (onBeginBarOperDelegate, inValues, onEndBarOperDelegate, onBarOperCompletedDelegate, userState);
  589. inArgs.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  590. var args = new List<CodeExpression> ();
  591. args.Add (beginOperDelegateRef);
  592. args.Add (new CodeArrayCreateExpression (typeof (object), inArgs.ConvertAll<CodeExpression> (decl => new CodeArgumentReferenceExpression (decl.Name)).ToArray ()));
  593. args.Add (endOperDelegateRef);
  594. args.Add (new CodeFieldReferenceExpression (thisExpr, "on" + od.Name + "CompletedDelegate"));
  595. args.Add (new CodeArgumentReferenceExpression ("userState"));
  596. call = new CodeMethodInvokeExpression (baseExpr, "InvokeAsync", args.ToArray ());
  597. cm.Statements.Add (new CodeExpressionStatement (call));
  598. }
  599. }
  600. void AddMethodParam (CodeMemberMethod cm, Type type, string name)
  601. {
  602. cm.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference (type), name));
  603. }
  604. const string ms_arrays_ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
  605. string GetCodeTypeName (QName mappedTypeName)
  606. {
  607. if (mappedTypeName.Namespace == ms_arrays_ns)
  608. return DataContractSerializerMessageContractImporter.GetCLRTypeName (mappedTypeName.Name.Substring ("ArrayOf".Length)) + "[]";
  609. return mappedTypeName.Name;
  610. }
  611. private CodeExpression[] ExportMessages (MessageDescriptionCollection messages, CodeMemberMethod method, bool return_args)
  612. {
  613. CodeExpression [] args = null;
  614. foreach (MessageDescription md in messages) {
  615. if (md.Direction == MessageDirection.Output) {
  616. if (md.Body.ReturnValue != null) {
  617. ExportDataContract (md.Body.ReturnValue.XmlTypeMapping);
  618. method.ReturnType = new CodeTypeReference (GetCodeTypeName (md.Body.ReturnValue.TypeName));
  619. }
  620. continue;
  621. }
  622. if (return_args)
  623. args = new CodeExpression [md.Body.Parts.Count];
  624. MessagePartDescriptionCollection parts = md.Body.Parts;
  625. for (int i = 0; i < parts.Count; i++) {
  626. ExportDataContract (parts [i].XmlTypeMapping);
  627. method.Parameters.Add (
  628. new CodeParameterDeclarationExpression (
  629. new CodeTypeReference (GetCodeTypeName (parts [i].TypeName)),
  630. parts [i].Name));
  631. if (return_args)
  632. args [i] = new CodeArgumentReferenceExpression (parts [i].Name);
  633. }
  634. }
  635. return args;
  636. }
  637. #endregion
  638. [MonoTODO]
  639. public CodeTypeReference GenerateServiceEndpoint (
  640. ServiceEndpoint endpoint,
  641. out ChannelEndpointElement channelElement)
  642. {
  643. throw new NotImplementedException ();
  644. }
  645. private void ExportDataContract (XmlTypeMapping mapping)
  646. {
  647. if (mapping == null)
  648. return;
  649. QName qname = new QName (mapping.TypeName, mapping.Namespace);
  650. if (imported_names.ContainsKey (qname))
  651. return;
  652. CodeNamespace cns = new CodeNamespace ();
  653. XmlCodeExporter xce = new XmlCodeExporter (cns);
  654. xce.ExportTypeMapping (mapping);
  655. List <CodeTypeDeclaration> to_remove = new List <CodeTypeDeclaration> ();
  656. //Process the types just generated
  657. //FIXME: Iterate and assign the types to correct namespaces
  658. //At the end, add all those namespaces to the ccu
  659. foreach (CodeTypeDeclaration type in cns.Types) {
  660. string ns = GetXmlNamespace (type);
  661. if (ns == null)
  662. //FIXME: do what here?
  663. continue;
  664. QName type_name = new QName (type.Name, ns);
  665. if (imported_names.ContainsKey (type_name)) {
  666. //Type got reemitted, so remove it!
  667. to_remove.Add (type);
  668. continue;
  669. }
  670. if (ns == ms_arrays_ns) {
  671. //Do not emit arrays as an independent type.
  672. to_remove.Add (type);
  673. continue;
  674. }
  675. imported_names [type_name] = type_name;
  676. type.Comments.Clear ();
  677. //Custom Attributes
  678. type.CustomAttributes.Clear ();
  679. if (type.IsEnum)
  680. continue;
  681. type.CustomAttributes.Add (
  682. new CodeAttributeDeclaration (
  683. new CodeTypeReference ("System.CodeDom.Compiler.GeneratedCodeAttribute"),
  684. new CodeAttributeArgument (new CodePrimitiveExpression ("System.Runtime.Serialization")),
  685. new CodeAttributeArgument (new CodePrimitiveExpression ("3.0.0.0"))));
  686. type.CustomAttributes.Add (
  687. new CodeAttributeDeclaration (
  688. new CodeTypeReference ("System.Runtime.Serialization.DataContractAttribute")));
  689. //BaseType and interface
  690. type.BaseTypes.Add (new CodeTypeReference (typeof (object)));
  691. type.BaseTypes.Add (new CodeTypeReference ("System.Runtime.Serialization.IExtensibleDataObject"));
  692. foreach (CodeTypeMember mbr in type.Members) {
  693. CodeMemberProperty p = mbr as CodeMemberProperty;
  694. if (p == null)
  695. continue;
  696. if ((p.Attributes & MemberAttributes.Public) == MemberAttributes.Public) {
  697. //FIXME: Clear all attributes or only XmlElementAttribute?
  698. p.CustomAttributes.Clear ();
  699. p.CustomAttributes.Add (new CodeAttributeDeclaration (
  700. new CodeTypeReference ("System.Runtime.Serialization.DataMemberAttribute")));
  701. p.Comments.Clear ();
  702. }
  703. }
  704. //Fields
  705. CodeMemberField field = new CodeMemberField (
  706. new CodeTypeReference ("System.Runtime.Serialization.ExtensionDataObject"),
  707. "extensionDataField");
  708. field.Attributes = MemberAttributes.Private | MemberAttributes.Final;
  709. type.Members.Add (field);
  710. //Property
  711. CodeMemberProperty prop = new CodeMemberProperty ();
  712. prop.Type = new CodeTypeReference ("System.Runtime.Serialization.ExtensionDataObject");
  713. prop.Name = "ExtensionData";
  714. prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  715. //Get
  716. prop.GetStatements.Add (new CodeMethodReturnStatement (
  717. new CodeFieldReferenceExpression (
  718. new CodeThisReferenceExpression (),
  719. "extensionDataField")));
  720. //Set
  721. prop.SetStatements.Add (new CodeAssignStatement (
  722. new CodeFieldReferenceExpression (
  723. new CodeThisReferenceExpression (),
  724. "extensionDataField"),
  725. new CodePropertySetValueReferenceExpression ()));
  726. type.Members.Add (prop);
  727. }
  728. foreach (CodeTypeDeclaration type in to_remove)
  729. cns.Types.Remove (type);
  730. ccu.Namespaces.Add (cns);
  731. }
  732. private string GetXmlNamespace (CodeTypeDeclaration type)
  733. {
  734. foreach (CodeAttributeDeclaration attr in type.CustomAttributes) {
  735. if (attr.Name == "System.Xml.Serialization.XmlTypeAttribute" ||
  736. attr.Name == "System.Xml.Serialization.XmlRootAttribute") {
  737. foreach (CodeAttributeArgument arg in attr.Arguments)
  738. if (arg.Name == "Namespace")
  739. return ((CodePrimitiveExpression)arg.Value).Value as string;
  740. //Could not find Namespace arg!
  741. return null;
  742. }
  743. }
  744. return null;
  745. }
  746. }
  747. }