MetadataSamples.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // MetadataProvider.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  6. //
  7. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. using System.Reflection;
  29. using System.ServiceModel;
  30. using System.ServiceModel.Security;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Description;
  33. using System.ServiceModel.Configuration;
  34. using WS = System.Web.Services.Description;
  35. namespace MonoTests.System.ServiceModel.MetadataTests {
  36. public static class MetadataSamples {
  37. internal const string HttpUri = "http://tempuri.org/TestHttp/";
  38. internal const string HttpsUri = "https://tempuri.org/TestHttps/";
  39. internal const string NetTcpUri = "net-tcp://tempuri.org:8000/TestNetTcp/";
  40. internal const string CustomUri = "custom://tempuri.org:8000/Test/";
  41. [MetadataSample]
  42. public static MetadataSet BasicHttp ()
  43. {
  44. var exporter = new WsdlExporter ();
  45. var cd = new ContractDescription ("MyContract");
  46. exporter.ExportEndpoint (new ServiceEndpoint (
  47. cd, new BasicHttpBinding (), new EndpointAddress (HttpUri)));
  48. return exporter.GetGeneratedMetadata ();
  49. }
  50. [MetadataSample]
  51. public static MetadataSet BasicHttp_TransportSecurity ()
  52. {
  53. var exporter = new WsdlExporter ();
  54. var cd = new ContractDescription ("MyContract");
  55. var binding = new BasicHttpBinding ();
  56. binding.Security.Mode = BasicHttpSecurityMode.Transport;
  57. exporter.ExportEndpoint (new ServiceEndpoint (
  58. cd, binding, new EndpointAddress (HttpUri)));
  59. return exporter.GetGeneratedMetadata ();
  60. }
  61. [MetadataSample]
  62. public static MetadataSet BasicHttp_MessageSecurity ()
  63. {
  64. var exporter = new WsdlExporter ();
  65. var cd = new ContractDescription ("MyContract");
  66. var binding = new BasicHttpBinding ();
  67. binding.Security.Mode = BasicHttpSecurityMode.Message;
  68. binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
  69. exporter.ExportEndpoint (new ServiceEndpoint (
  70. cd, binding, new EndpointAddress (HttpUri)));
  71. return exporter.GetGeneratedMetadata ();
  72. }
  73. [MetadataSample]
  74. public static MetadataSet BasicHttp_TransportWithMessageCredential ()
  75. {
  76. var exporter = new WsdlExporter ();
  77. var cd = new ContractDescription ("MyContract");
  78. var binding = new BasicHttpBinding ();
  79. binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
  80. exporter.ExportEndpoint (new ServiceEndpoint (
  81. cd, binding, new EndpointAddress (HttpUri)));
  82. return exporter.GetGeneratedMetadata ();
  83. }
  84. [MetadataSample]
  85. public static MetadataSet BasicHttp_Mtom ()
  86. {
  87. var exporter = new WsdlExporter ();
  88. var cd = new ContractDescription ("MyContract");
  89. var binding = new BasicHttpBinding ();
  90. binding.MessageEncoding = WSMessageEncoding.Mtom;
  91. exporter.ExportEndpoint (new ServiceEndpoint (
  92. cd, binding, new EndpointAddress (HttpUri)));
  93. return exporter.GetGeneratedMetadata ();
  94. }
  95. [MetadataSample]
  96. public static MetadataSet BasicHttp_NtlmAuth ()
  97. {
  98. var exporter = new WsdlExporter ();
  99. var cd = new ContractDescription ("MyContract");
  100. var binding = new BasicHttpBinding (BasicHttpSecurityMode.TransportCredentialOnly);
  101. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
  102. exporter.ExportEndpoint (new ServiceEndpoint (
  103. cd, binding, new EndpointAddress (HttpUri)));
  104. return exporter.GetGeneratedMetadata ();
  105. }
  106. [MetadataSample]
  107. public static MetadataSet BasicHttps ()
  108. {
  109. var exporter = new WsdlExporter ();
  110. var cd = new ContractDescription ("MyContract");
  111. exporter.ExportEndpoint (new ServiceEndpoint (
  112. cd, new BasicHttpsBinding (), new EndpointAddress (HttpsUri)));
  113. return exporter.GetGeneratedMetadata ();
  114. }
  115. [MetadataSample]
  116. public static MetadataSet BasicHttps_NtlmAuth ()
  117. {
  118. var exporter = new WsdlExporter ();
  119. var cd = new ContractDescription ("MyContract");
  120. var binding = new BasicHttpsBinding ();
  121. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
  122. exporter.ExportEndpoint (new ServiceEndpoint (
  123. cd, binding, new EndpointAddress (HttpsUri)));
  124. return exporter.GetGeneratedMetadata ();
  125. }
  126. [MetadataSample]
  127. public static MetadataSet BasicHttps_Certificate ()
  128. {
  129. var exporter = new WsdlExporter ();
  130. var cd = new ContractDescription ("MyContract");
  131. var binding = new BasicHttpsBinding ();
  132. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
  133. exporter.ExportEndpoint (new ServiceEndpoint (
  134. cd, binding, new EndpointAddress (HttpsUri)));
  135. return exporter.GetGeneratedMetadata ();
  136. }
  137. [MetadataSample]
  138. public static MetadataSet BasicHttps_TransportWithMessageCredential ()
  139. {
  140. var exporter = new WsdlExporter ();
  141. var cd = new ContractDescription ("MyContract");
  142. var binding = new BasicHttpsBinding (BasicHttpsSecurityMode.TransportWithMessageCredential);
  143. exporter.ExportEndpoint (new ServiceEndpoint (
  144. cd, binding, new EndpointAddress (HttpsUri)));
  145. return exporter.GetGeneratedMetadata ();
  146. }
  147. [MetadataSample]
  148. public static MetadataSet NetTcp ()
  149. {
  150. var exporter = new WsdlExporter ();
  151. var cd = new ContractDescription ("MyContract");
  152. exporter.ExportEndpoint (new ServiceEndpoint (
  153. cd, new NetTcpBinding (SecurityMode.None, false),
  154. new EndpointAddress (NetTcpUri)));
  155. return exporter.GetGeneratedMetadata ();
  156. }
  157. [MetadataSample]
  158. public static MetadataSet NetTcp_TransportSecurity ()
  159. {
  160. var exporter = new WsdlExporter ();
  161. var cd = new ContractDescription ("MyContract");
  162. exporter.ExportEndpoint (new ServiceEndpoint (
  163. cd, new NetTcpBinding (SecurityMode.Transport, false),
  164. new EndpointAddress (NetTcpUri)));
  165. return exporter.GetGeneratedMetadata ();
  166. }
  167. [MetadataSample]
  168. public static MetadataSet NetTcp_MessageSecurity ()
  169. {
  170. var exporter = new WsdlExporter ();
  171. var cd = new ContractDescription ("MyContract");
  172. exporter.ExportEndpoint (new ServiceEndpoint (
  173. cd, new NetTcpBinding (SecurityMode.Message, false),
  174. new EndpointAddress (NetTcpUri)));
  175. return exporter.GetGeneratedMetadata ();
  176. }
  177. [MetadataSample]
  178. public static MetadataSet NetTcp_TransportWithMessageCredential ()
  179. {
  180. var exporter = new WsdlExporter ();
  181. var cd = new ContractDescription ("MyContract");
  182. exporter.ExportEndpoint (new ServiceEndpoint (
  183. cd, new NetTcpBinding (SecurityMode.TransportWithMessageCredential, false),
  184. new EndpointAddress (NetTcpUri)));
  185. return exporter.GetGeneratedMetadata ();
  186. }
  187. [MetadataSample]
  188. public static MetadataSet NetTcp_ReliableSession ()
  189. {
  190. var exporter = new WsdlExporter ();
  191. var cd = new ContractDescription ("MyContract");
  192. var binding = new NetTcpBinding (SecurityMode.None, true);
  193. exporter.ExportEndpoint (new ServiceEndpoint (
  194. cd, binding, new EndpointAddress (NetTcpUri)));
  195. return exporter.GetGeneratedMetadata ();
  196. }
  197. [MetadataSample]
  198. public static MetadataSet NetTcp_TransferMode ()
  199. {
  200. var exporter = new WsdlExporter ();
  201. var cd = new ContractDescription ("MyContract");
  202. var binding = new NetTcpBinding (SecurityMode.None, false);
  203. binding.TransferMode = TransferMode.Streamed;
  204. exporter.ExportEndpoint (new ServiceEndpoint (
  205. cd, binding, new EndpointAddress (NetTcpUri)));
  206. return exporter.GetGeneratedMetadata ();
  207. }
  208. [ServiceContract]
  209. public interface IMyContract {
  210. [OperationContract]
  211. void Hello ();
  212. }
  213. [MetadataSample]
  214. public static MetadataSet BasicHttp_Operation ()
  215. {
  216. var exporter = new WsdlExporter ();
  217. var cd = ContractDescription.GetContract (typeof (IMyContract));
  218. var binding = new BasicHttpBinding ();
  219. exporter.ExportEndpoint (new ServiceEndpoint (
  220. cd, binding, new EndpointAddress (HttpUri)));
  221. return exporter.GetGeneratedMetadata ();
  222. }
  223. [MetadataSample]
  224. public static MetadataSet NetTcp_Operation ()
  225. {
  226. var exporter = new WsdlExporter ();
  227. var cd = ContractDescription.GetContract (typeof (IMyContract));
  228. exporter.ExportEndpoint (new ServiceEndpoint (
  229. cd, new NetTcpBinding (SecurityMode.None, false),
  230. new EndpointAddress (NetTcpUri)));
  231. return exporter.GetGeneratedMetadata ();
  232. }
  233. [MetadataSample (CreateConfig = true)]
  234. public static MetadataSet BasicHttp_Config ()
  235. {
  236. var exporter = new WsdlExporter ();
  237. var cd = ContractDescription.GetContract (typeof (IMyContract));
  238. var binding = new BasicHttpBinding ();
  239. exporter.ExportEndpoint (new ServiceEndpoint (
  240. cd, binding, new EndpointAddress (HttpUri)));
  241. return exporter.GetGeneratedMetadata ();
  242. }
  243. [MetadataSample (CreateConfig = true)]
  244. public static MetadataSet BasicHttp_Config2 ()
  245. {
  246. var exporter = new WsdlExporter ();
  247. var cd = ContractDescription.GetContract (typeof (IMyContract));
  248. exporter.ExportEndpoint (new ServiceEndpoint (
  249. cd, new BasicHttpBinding (),
  250. new EndpointAddress (HttpUri)));
  251. exporter.ExportEndpoint (new ServiceEndpoint (
  252. cd, new NetTcpBinding (SecurityMode.None, false),
  253. new EndpointAddress (NetTcpUri)));
  254. return exporter.GetGeneratedMetadata ();
  255. }
  256. #region Helper API
  257. public static void Export (string outputDir)
  258. {
  259. if (!Directory.Exists (outputDir))
  260. Directory.CreateDirectory (outputDir);
  261. var bf = BindingFlags.Public | BindingFlags.Static;
  262. foreach (var method in typeof (MetadataSamples).GetMethods (bf)) {
  263. MetadataSampleAttribute sampleAttr = null;
  264. foreach (var obj in method.GetCustomAttributes (false)) {
  265. var cattr = obj as MetadataSampleAttribute;
  266. if (cattr != null) {
  267. sampleAttr = cattr;
  268. break;
  269. }
  270. }
  271. if (sampleAttr == null)
  272. continue;
  273. var name = sampleAttr.Name ?? method.Name;
  274. var metadata = (MetadataSet)method.Invoke (null, null);
  275. var xmlFilename = Path.Combine (outputDir, name + ".xml");
  276. TestContext.SaveMetadata (xmlFilename, metadata);
  277. if (!sampleAttr.CreateConfig)
  278. continue;
  279. var configFilename = Path.Combine (outputDir, name + ".config");
  280. TestContext.GenerateConfig (configFilename, metadata);
  281. }
  282. }
  283. public static MetadataSet GetMetadataByName (string name)
  284. {
  285. if (name.EndsWith (".xml"))
  286. name = name.Substring (name.Length - 4);
  287. var bf = BindingFlags.Public | BindingFlags.Static;
  288. foreach (var method in typeof (MetadataSamples).GetMethods (bf)) {
  289. MetadataSampleAttribute sampleAttr = null;
  290. foreach (var obj in method.GetCustomAttributes (false)) {
  291. var cattr = obj as MetadataSampleAttribute;
  292. if (cattr != null) {
  293. sampleAttr = cattr;
  294. break;
  295. }
  296. }
  297. if (sampleAttr == null)
  298. continue;
  299. if (!name.Equals (sampleAttr.Name ?? method.Name))
  300. continue;
  301. return (MetadataSet)method.Invoke (null, null);
  302. }
  303. throw new InvalidOperationException (string.Format (
  304. "No such metadata sample: '{0}'", name));
  305. }
  306. public class MetadataSampleAttribute : Attribute {
  307. public MetadataSampleAttribute ()
  308. {
  309. }
  310. public MetadataSampleAttribute (string name)
  311. {
  312. Name = name;
  313. }
  314. public string Name {
  315. get; set;
  316. }
  317. public bool CreateConfig {
  318. get; set;
  319. }
  320. }
  321. #endregion
  322. }
  323. }