ServiceContractGenerator.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. //
  2. // ServiceContractGenerator.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. CodeMemberMethod cm = new CodeMemberMethod ();
  282. type.Members.Add (cm);
  283. if (GenerateAsync) {
  284. cm.Name = "Begin" + od.Name;
  285. beginMethod = cm;
  286. } else {
  287. cm.Name = od.Name;
  288. syncMethod = cm;
  289. }
  290. CodeTypeReference returnTypeFromMessageContract = null;
  291. if (od.SyncMethod != null) {
  292. ExportParameters (cm, od.SyncMethod.GetParameters ());
  293. if (GenerateAsync) {
  294. AddBeginAsyncArgs (cm);
  295. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  296. }
  297. else
  298. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  299. } else {
  300. ExportMessages (od.Messages, cm, false);
  301. returnTypeFromMessageContract = cm.ReturnType;
  302. if (GenerateAsync) {
  303. AddBeginAsyncArgs (cm);
  304. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  305. }
  306. }
  307. // [OperationContract (Action = "...", ReplyAction = "..")]
  308. CodeAttributeDeclaration ad =
  309. new CodeAttributeDeclaration (
  310. new CodeTypeReference (
  311. typeof (OperationContractAttribute)));
  312. foreach (MessageDescription md in od.Messages) {
  313. if (md.Direction == MessageDirection.Input)
  314. ad.Arguments.Add (new CodeAttributeArgument ("Action", new CodePrimitiveExpression (md.Action)));
  315. else
  316. ad.Arguments.Add (new CodeAttributeArgument ("ReplyAction", new CodePrimitiveExpression (md.Action)));
  317. }
  318. if (GenerateAsync)
  319. ad.Arguments.Add (new CodeAttributeArgument ("AsyncPattern", new CodePrimitiveExpression (true)));
  320. cm.CustomAttributes.Add (ad);
  321. // For async mode, add EndXxx() too.
  322. if (GenerateAsync) {
  323. cm = new CodeMemberMethod ();
  324. type.Members.Add (cm);
  325. cm.Name = "End" + od.Name;
  326. endMethod = cm;
  327. var res = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (IAsyncResult)), "result");
  328. cm.Parameters.Add (res);
  329. if (od.SyncMethod != null) // FIXME: it depends on sync method!
  330. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  331. else
  332. cm.ReturnType = returnTypeFromMessageContract;
  333. }
  334. foreach (var ob in od.Behaviors) {
  335. var gex = ob as IOperationContractGenerationExtension;
  336. if (gex != null)
  337. operation_contexts.Add (new OPair (gex, new OperationContractGenerationContext (this, contract_context, od, type, syncMethod, beginMethod, endMethod)));
  338. }
  339. }
  340. }
  341. void ExportParameters (CodeMemberMethod method, ParameterInfo [] parameters)
  342. {
  343. foreach (ParameterInfo pi in parameters)
  344. method.Parameters.Add (
  345. new CodeParameterDeclarationExpression (
  346. new CodeTypeReference (pi.ParameterType),
  347. pi.Name));
  348. }
  349. void AddImplementationClientMethods (CodeTypeDeclaration type, ContractDescription cd)
  350. {
  351. foreach (OperationDescription od in cd.Operations) {
  352. CodeMemberMethod cm = new CodeMemberMethod ();
  353. type.Members.Add (cm);
  354. if (GenerateAsync)
  355. cm.Name = "Begin" + od.Name;
  356. else
  357. cm.Name = od.Name;
  358. cm.Attributes = MemberAttributes.Public
  359. | MemberAttributes.Final;
  360. CodeTypeReference returnTypeFromMessageContract = null;
  361. List<CodeExpression> args = new List<CodeExpression> ();
  362. if (od.SyncMethod != null) {
  363. ParameterInfo [] pars = od.SyncMethod.GetParameters ();
  364. ExportParameters (cm, pars);
  365. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  366. int i = 0;
  367. foreach (ParameterInfo pi in pars)
  368. args.Add (new CodeArgumentReferenceExpression (pi.Name));
  369. } else {
  370. args.AddRange (ExportMessages (od.Messages, cm, true));
  371. returnTypeFromMessageContract = cm.ReturnType;
  372. if (GenerateAsync) {
  373. AddBeginAsyncArgs (cm);
  374. cm.ReturnType = new CodeTypeReference (typeof (IAsyncResult));
  375. }
  376. }
  377. if (GenerateAsync) {
  378. args.Add (new CodeArgumentReferenceExpression ("asyncCallback"));
  379. args.Add (new CodeArgumentReferenceExpression ("userState"));
  380. }
  381. CodeExpression call = new CodeMethodInvokeExpression (
  382. new CodePropertyReferenceExpression (
  383. new CodeBaseReferenceExpression (),
  384. "Channel"),
  385. cm.Name,
  386. args.ToArray ());
  387. if (cm.ReturnType.BaseType == "System.Void")
  388. cm.Statements.Add (new CodeExpressionStatement (call));
  389. else
  390. cm.Statements.Add (new CodeMethodReturnStatement (call));
  391. // For async mode, add EndXxx() too.
  392. if (!GenerateAsync)
  393. continue;
  394. // EndXxx() implementation
  395. cm = new CodeMemberMethod ();
  396. cm.Attributes = MemberAttributes.Public
  397. | MemberAttributes.Final;
  398. type.Members.Add (cm);
  399. cm.Name = "End" + od.Name;
  400. var res = new CodeParameterDeclarationExpression (new CodeTypeReference (typeof (IAsyncResult)), "result");
  401. cm.Parameters.Add (res);
  402. if (od.SyncMethod != null) // FIXME: it depends on sync method!
  403. cm.ReturnType = new CodeTypeReference (od.SyncMethod.ReturnType);
  404. else
  405. cm.ReturnType = returnTypeFromMessageContract;
  406. string resultArgName = "result";
  407. if (od.EndMethod != null)
  408. resultArgName = od.EndMethod.GetParameters () [0].Name;
  409. call = new CodeMethodInvokeExpression (
  410. new CodePropertyReferenceExpression (
  411. new CodeBaseReferenceExpression (),
  412. "Channel"),
  413. cm.Name,
  414. new CodeArgumentReferenceExpression (resultArgName));
  415. if (cm.ReturnType.BaseType == "System.Void")
  416. cm.Statements.Add (new CodeExpressionStatement (call));
  417. else
  418. cm.Statements.Add (new CodeMethodReturnStatement (call));
  419. }
  420. }
  421. CodeMemberMethod FindByName (CodeTypeDeclaration type, string name)
  422. {
  423. foreach (var m in type.Members) {
  424. var method = m as CodeMemberMethod;
  425. if (method != null && method.Name == name)
  426. return method;
  427. }
  428. return null;
  429. }
  430. void GenerateEventBasedAsyncSupport (CodeTypeDeclaration type, OperationDescription od, CodeNamespace cns)
  431. {
  432. var method = FindByName (type, od.Name) ?? FindByName (type, "Begin" + od.Name);
  433. var endMethod = method.Name == od.Name ? null : FindByName (type, "End" + od.Name);
  434. bool methodAsync = method.Name.StartsWith ("Begin", StringComparison.Ordinal);
  435. var thisExpr = new CodeThisReferenceExpression ();
  436. var baseExpr = new CodeBaseReferenceExpression ();
  437. var nullExpr = new CodePrimitiveExpression (null);
  438. var asyncResultType = new CodeTypeReference (typeof (IAsyncResult));
  439. // OnBeginXxx() implementation
  440. var cm = new CodeMemberMethod () {
  441. Name = "OnBegin" + od.Name,
  442. Attributes = MemberAttributes.Private | MemberAttributes.Final,
  443. ReturnType = asyncResultType
  444. };
  445. type.Members.Add (cm);
  446. AddMethodParam (cm, typeof (object []), "args");
  447. AddMethodParam (cm, typeof (AsyncCallback), "asyncCallback");
  448. AddMethodParam (cm, typeof (object), "userState");
  449. var call = new CodeMethodInvokeExpression (
  450. thisExpr,
  451. "Begin" + od.Name);
  452. for (int idx = 0; idx < method.Parameters.Count - (methodAsync ? 2 : 0); idx++) {
  453. var p = method.Parameters [idx];
  454. cm.Statements.Add (new CodeVariableDeclarationStatement (p.Type, p.Name, new CodeCastExpression (p.Type, new CodeArrayIndexerExpression (new CodeArgumentReferenceExpression ("args"), new CodePrimitiveExpression (idx)))));
  455. call.Parameters.Add (new CodeVariableReferenceExpression (p.Name));
  456. }
  457. call.Parameters.Add (new CodeArgumentReferenceExpression ("asyncCallback"));
  458. call.Parameters.Add (new CodeArgumentReferenceExpression ("userState"));
  459. cm.Statements.Add (new CodeMethodReturnStatement (call));
  460. // OnEndXxx() implementation
  461. cm = new CodeMemberMethod () {
  462. Name = "OnEnd" + od.Name,
  463. Attributes = MemberAttributes.Private | MemberAttributes.Final,
  464. ReturnType = new CodeTypeReference (typeof (object [])) };
  465. type.Members.Add (cm);
  466. AddMethodParam (cm, typeof (IAsyncResult), "result");
  467. var outArgRefs = new List<CodeVariableReferenceExpression> ();
  468. for (int idx = 0; idx < method.Parameters.Count; idx++) {
  469. var p = method.Parameters [idx];
  470. if (p.Direction != FieldDirection.In) {
  471. cm.Statements.Add (new CodeVariableDeclarationStatement (p.Type, p.Name));
  472. outArgRefs.Add (new CodeVariableReferenceExpression (p.Name)); // FIXME: should this work? They need "out" or "ref" modifiers.
  473. }
  474. }
  475. call = new CodeMethodInvokeExpression (
  476. thisExpr,
  477. "End" + od.Name,
  478. new CodeArgumentReferenceExpression ("result"));
  479. call.Parameters.AddRange (outArgRefs.Cast<CodeExpression> ().ToArray ()); // questionable
  480. cm.Statements.Add (new CodeVariableDeclarationStatement (typeof (object), "__ret", call));
  481. var retCreate = new CodeArrayCreateExpression (typeof (object));
  482. retCreate.Initializers.Add (new CodeVariableReferenceExpression ("__ret"));
  483. foreach (var outArgRef in outArgRefs)
  484. retCreate.Initializers.Add (new CodeVariableReferenceExpression (outArgRef.VariableName));
  485. cm.Statements.Add (new CodeMethodReturnStatement (retCreate));
  486. // OnXxxCompleted() implementation
  487. cm = new CodeMemberMethod () {
  488. Name = "On" + od.Name + "Completed",
  489. Attributes = MemberAttributes.Private | MemberAttributes.Final };
  490. type.Members.Add (cm);
  491. AddMethodParam (cm, typeof (object), "state");
  492. var iaargs = new CodeTypeReference ("InvokeAsyncCompletedEventArgs"); // avoid messy System.Type instance for generic nested type :|
  493. var iaref = new CodeVariableReferenceExpression ("args");
  494. var methodEventArgs = new CodeObjectCreateExpression (new CodeTypeReference (od.Name + "CompletedEventArgs"),
  495. new CodePropertyReferenceExpression (iaref, "Results"),
  496. new CodePropertyReferenceExpression (iaref, "Error"),
  497. new CodePropertyReferenceExpression (iaref, "Cancelled"),
  498. new CodePropertyReferenceExpression (iaref, "UserState"));
  499. cm.Statements.Add (new CodeConditionStatement (
  500. new CodeBinaryOperatorExpression (
  501. new CodeEventReferenceExpression (thisExpr, od.Name + "Completed"), CodeBinaryOperatorType.IdentityInequality, nullExpr),
  502. new CodeVariableDeclarationStatement (iaargs, "args", new CodeCastExpression (iaargs, new CodeArgumentReferenceExpression ("state"))),
  503. new CodeExpressionStatement (new CodeMethodInvokeExpression (thisExpr, od.Name + "Completed", thisExpr, methodEventArgs))));
  504. // delegate fields
  505. type.Members.Add (new CodeMemberField (new CodeTypeReference ("BeginOperationDelegate"), "onBegin" + od.Name + "Delegate"));
  506. type.Members.Add (new CodeMemberField (new CodeTypeReference ("EndOperationDelegate"), "onEnd" + od.Name + "Delegate"));
  507. type.Members.Add (new CodeMemberField (new CodeTypeReference (typeof (SendOrPostCallback)), "on" + od.Name + "CompletedDelegate"));
  508. // XxxCompletedEventArgs class
  509. var argsType = new CodeTypeDeclaration (od.Name + "CompletedEventArgs");
  510. argsType.BaseTypes.Add (new CodeTypeReference (typeof (AsyncCompletedEventArgs)));
  511. cns.Types.Add (argsType);
  512. var argsCtor = new CodeConstructor () {
  513. Attributes = MemberAttributes.Public | MemberAttributes.Final };
  514. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object []), "results"));
  515. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (Exception), "error"));
  516. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (bool), "cancelled"));
  517. argsCtor.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  518. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("error"));
  519. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("cancelled"));
  520. argsCtor.BaseConstructorArgs.Add (new CodeArgumentReferenceExpression ("userState"));
  521. var resultsField = new CodeFieldReferenceExpression (thisExpr, "results");
  522. argsCtor.Statements.Add (new CodeAssignStatement (resultsField, new CodeArgumentReferenceExpression ("results")));
  523. argsType.Members.Add (argsCtor);
  524. argsType.Members.Add (new CodeMemberField (typeof (object []), "results"));
  525. var resultProp = new CodeMemberProperty {
  526. Name = "Result",
  527. Type = endMethod != null ? endMethod.ReturnType : method.ReturnType,
  528. Attributes = MemberAttributes.Public | MemberAttributes.Final };
  529. resultProp.GetStatements.Add (new CodeMethodReturnStatement (new CodeCastExpression (resultProp.Type, new CodeArrayIndexerExpression (resultsField, new CodePrimitiveExpression (0)))));
  530. argsType.Members.Add (resultProp);
  531. // event field
  532. var handlerType = new CodeTypeReference (typeof (EventHandler<>));
  533. handlerType.TypeArguments.Add (new CodeTypeReference (argsType.Name));
  534. type.Members.Add (new CodeMemberEvent () {
  535. Name = od.Name + "Completed",
  536. Type = handlerType,
  537. Attributes = MemberAttributes.Public | MemberAttributes.Final });
  538. // XxxAsync() implementations
  539. bool hasAsync = false;
  540. foreach (int __x in Enumerable.Range (0, 2)) {
  541. cm = new CodeMemberMethod ();
  542. type.Members.Add (cm);
  543. cm.Name = od.Name + "Async";
  544. cm.Attributes = MemberAttributes.Public
  545. | MemberAttributes.Final;
  546. var inArgs = new List<CodeParameterDeclarationExpression > ();
  547. for (int idx = 0; idx < method.Parameters.Count - (methodAsync ? 2 : 0); idx++) {
  548. var pd = method.Parameters [idx];
  549. inArgs.Add (pd);
  550. cm.Parameters.Add (pd);
  551. }
  552. // First one is overload without asyncState arg.
  553. if (!hasAsync) {
  554. call = new CodeMethodInvokeExpression (thisExpr, cm.Name, inArgs.ConvertAll<CodeExpression> (decl => new CodeArgumentReferenceExpression (decl.Name)).ToArray ());
  555. call.Parameters.Add (nullExpr);
  556. cm.Statements.Add (new CodeExpressionStatement (call));
  557. hasAsync = true;
  558. continue;
  559. }
  560. // Second one is the primary one.
  561. cm.Parameters.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  562. // if (onBeginBarOperDelegate == null) onBeginBarOperDelegate = new BeginOperationDelegate (OnBeginBarOper);
  563. // if (onEndBarOperDelegate == null) onEndBarOperDelegate = new EndOperationDelegate (OnEndBarOper);
  564. // if (onBarOperCompletedDelegate == null) onBarOperCompletedDelegate = new BeginOperationDelegate (OnBarOperCompleted);
  565. var beginOperDelegateRef = new CodeFieldReferenceExpression (thisExpr, "onBegin" + od.Name + "Delegate");
  566. var endOperDelegateRef = new CodeFieldReferenceExpression (thisExpr, "onEnd" + od.Name + "Delegate");
  567. var operCompletedDelegateRef = new CodeFieldReferenceExpression (thisExpr, "on" + od.Name + "CompletedDelegate");
  568. var ifstmt = new CodeConditionStatement (
  569. new CodeBinaryOperatorExpression (beginOperDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  570. new CodeAssignStatement (beginOperDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference ("BeginOperationDelegate"), thisExpr, "OnBegin" + od.Name)));
  571. cm.Statements.Add (ifstmt);
  572. ifstmt = new CodeConditionStatement (
  573. new CodeBinaryOperatorExpression (endOperDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  574. new CodeAssignStatement (endOperDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference ("EndOperationDelegate"), thisExpr, "OnEnd" + od.Name)));
  575. cm.Statements.Add (ifstmt);
  576. ifstmt = new CodeConditionStatement (
  577. new CodeBinaryOperatorExpression (operCompletedDelegateRef, CodeBinaryOperatorType.IdentityEquality, nullExpr),
  578. new CodeAssignStatement (operCompletedDelegateRef, new CodeDelegateCreateExpression (new CodeTypeReference (typeof (SendOrPostCallback)), thisExpr, "On" + od.Name + "Completed")));
  579. cm.Statements.Add (ifstmt);
  580. // InvokeAsync (onBeginBarOperDelegate, inValues, onEndBarOperDelegate, onBarOperCompletedDelegate, userState);
  581. inArgs.Add (new CodeParameterDeclarationExpression (typeof (object), "userState"));
  582. var args = new List<CodeExpression> ();
  583. args.Add (beginOperDelegateRef);
  584. args.Add (new CodeArrayCreateExpression (typeof (object), inArgs.ConvertAll<CodeExpression> (decl => new CodeArgumentReferenceExpression (decl.Name)).ToArray ()));
  585. args.Add (endOperDelegateRef);
  586. args.Add (new CodeFieldReferenceExpression (thisExpr, "on" + od.Name + "CompletedDelegate"));
  587. args.Add (new CodeArgumentReferenceExpression ("userState"));
  588. call = new CodeMethodInvokeExpression (baseExpr, "InvokeAsync", args.ToArray ());
  589. cm.Statements.Add (new CodeExpressionStatement (call));
  590. }
  591. }
  592. void AddMethodParam (CodeMemberMethod cm, Type type, string name)
  593. {
  594. cm.Parameters.Add (new CodeParameterDeclarationExpression (new CodeTypeReference (type), name));
  595. }
  596. private CodeExpression[] ExportMessages (MessageDescriptionCollection messages, CodeMemberMethod method, bool return_args)
  597. {
  598. CodeExpression [] args = null;
  599. foreach (MessageDescription md in messages) {
  600. if (md.Direction == MessageDirection.Output) {
  601. if (md.Body.ReturnValue != null) {
  602. ExportDataContract (md.Body.ReturnValue.XmlTypeMapping);
  603. method.ReturnType = new CodeTypeReference (md.Body.ReturnValue.TypeName.Name);
  604. }
  605. continue;
  606. }
  607. if (return_args)
  608. args = new CodeExpression [md.Body.Parts.Count];
  609. MessagePartDescriptionCollection parts = md.Body.Parts;
  610. for (int i = 0; i < parts.Count; i++) {
  611. ExportDataContract (parts [i].XmlTypeMapping);
  612. method.Parameters.Add (
  613. new CodeParameterDeclarationExpression (
  614. new CodeTypeReference (parts [i].TypeName.Name),
  615. parts [i].Name));
  616. if (return_args)
  617. args [i] = new CodeArgumentReferenceExpression (parts [i].Name);
  618. }
  619. }
  620. return args;
  621. }
  622. #endregion
  623. [MonoTODO]
  624. public CodeTypeReference GenerateServiceEndpoint (
  625. ServiceEndpoint endpoint,
  626. out ChannelEndpointElement channelElement)
  627. {
  628. throw new NotImplementedException ();
  629. }
  630. private void ExportDataContract (XmlTypeMapping mapping)
  631. {
  632. if (mapping == null)
  633. return;
  634. QName qname = new QName (mapping.TypeName, mapping.Namespace);
  635. if (imported_names.ContainsKey (qname))
  636. return;
  637. CodeNamespace cns = new CodeNamespace ();
  638. XmlCodeExporter xce = new XmlCodeExporter (cns);
  639. xce.ExportTypeMapping (mapping);
  640. List <CodeTypeDeclaration> to_remove = new List <CodeTypeDeclaration> ();
  641. //Process the types just generated
  642. //FIXME: Iterate and assign the types to correct namespaces
  643. //At the end, add all those namespaces to the ccu
  644. foreach (CodeTypeDeclaration type in cns.Types) {
  645. string ns = GetXmlNamespace (type);
  646. if (ns == null)
  647. //FIXME: do what here?
  648. continue;
  649. QName type_name = new QName (type.Name, ns);
  650. if (imported_names.ContainsKey (type_name)) {
  651. //Type got reemitted, so remove it!
  652. to_remove.Add (type);
  653. continue;
  654. }
  655. imported_names [type_name] = type_name;
  656. type.Comments.Clear ();
  657. //Custom Attributes
  658. type.CustomAttributes.Clear ();
  659. if (type.IsEnum)
  660. continue;
  661. type.CustomAttributes.Add (
  662. new CodeAttributeDeclaration (
  663. new CodeTypeReference ("System.CodeDom.Compiler.GeneratedCodeAttribute"),
  664. new CodeAttributeArgument (new CodePrimitiveExpression ("System.Runtime.Serialization")),
  665. new CodeAttributeArgument (new CodePrimitiveExpression ("3.0.0.0"))));
  666. type.CustomAttributes.Add (
  667. new CodeAttributeDeclaration (
  668. new CodeTypeReference ("System.Runtime.Serialization.DataContractAttribute")));
  669. //BaseType and interface
  670. type.BaseTypes.Add (new CodeTypeReference (typeof (object)));
  671. type.BaseTypes.Add (new CodeTypeReference ("System.Runtime.Serialization.IExtensibleDataObject"));
  672. foreach (CodeTypeMember mbr in type.Members) {
  673. CodeMemberProperty p = mbr as CodeMemberProperty;
  674. if (p == null)
  675. continue;
  676. if ((p.Attributes & MemberAttributes.Public) == MemberAttributes.Public) {
  677. //FIXME: Clear all attributes or only XmlElementAttribute?
  678. p.CustomAttributes.Clear ();
  679. p.CustomAttributes.Add (new CodeAttributeDeclaration (
  680. new CodeTypeReference ("System.Runtime.Serialization.DataMemberAttribute")));
  681. p.Comments.Clear ();
  682. }
  683. }
  684. //Fields
  685. CodeMemberField field = new CodeMemberField (
  686. new CodeTypeReference ("System.Runtime.Serialization.ExtensionDataObject"),
  687. "extensionDataField");
  688. field.Attributes = MemberAttributes.Private | MemberAttributes.Final;
  689. type.Members.Add (field);
  690. //Property
  691. CodeMemberProperty prop = new CodeMemberProperty ();
  692. prop.Type = new CodeTypeReference ("System.Runtime.Serialization.ExtensionDataObject");
  693. prop.Name = "ExtensionData";
  694. prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  695. //Get
  696. prop.GetStatements.Add (new CodeMethodReturnStatement (
  697. new CodeFieldReferenceExpression (
  698. new CodeThisReferenceExpression (),
  699. "extensionDataField")));
  700. //Set
  701. prop.SetStatements.Add (new CodeAssignStatement (
  702. new CodeFieldReferenceExpression (
  703. new CodeThisReferenceExpression (),
  704. "extensionDataField"),
  705. new CodePropertySetValueReferenceExpression ()));
  706. type.Members.Add (prop);
  707. }
  708. foreach (CodeTypeDeclaration type in to_remove)
  709. cns.Types.Remove (type);
  710. ccu.Namespaces.Add (cns);
  711. }
  712. private string GetXmlNamespace (CodeTypeDeclaration type)
  713. {
  714. foreach (CodeAttributeDeclaration attr in type.CustomAttributes) {
  715. if (attr.Name == "System.Xml.Serialization.XmlTypeAttribute" ||
  716. attr.Name == "System.Xml.Serialization.XmlRootAttribute") {
  717. foreach (CodeAttributeArgument arg in attr.Arguments)
  718. if (arg.Name == "Namespace")
  719. return ((CodePrimitiveExpression)arg.Value).Value as string;
  720. //Could not find Namespace arg!
  721. return null;
  722. }
  723. }
  724. return null;
  725. }
  726. }
  727. }