ServiceContractGenerator.cs 34 KB

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