MetadataSamples.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #if NET_4_5
  107. [MetadataSample]
  108. public static MetadataSet BasicHttps ()
  109. {
  110. var exporter = new WsdlExporter ();
  111. var cd = new ContractDescription ("MyContract");
  112. exporter.ExportEndpoint (new ServiceEndpoint (
  113. cd, new BasicHttpsBinding (), new EndpointAddress (HttpsUri)));
  114. return exporter.GetGeneratedMetadata ();
  115. }
  116. [MetadataSample]
  117. public static MetadataSet BasicHttps_NtlmAuth ()
  118. {
  119. var exporter = new WsdlExporter ();
  120. var cd = new ContractDescription ("MyContract");
  121. var binding = new BasicHttpsBinding ();
  122. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
  123. exporter.ExportEndpoint (new ServiceEndpoint (
  124. cd, binding, new EndpointAddress (HttpsUri)));
  125. return exporter.GetGeneratedMetadata ();
  126. }
  127. [MetadataSample]
  128. public static MetadataSet BasicHttps_Certificate ()
  129. {
  130. var exporter = new WsdlExporter ();
  131. var cd = new ContractDescription ("MyContract");
  132. var binding = new BasicHttpsBinding ();
  133. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
  134. exporter.ExportEndpoint (new ServiceEndpoint (
  135. cd, binding, new EndpointAddress (HttpsUri)));
  136. return exporter.GetGeneratedMetadata ();
  137. }
  138. [MetadataSample]
  139. public static MetadataSet BasicHttps_TransportWithMessageCredential ()
  140. {
  141. var exporter = new WsdlExporter ();
  142. var cd = new ContractDescription ("MyContract");
  143. var binding = new BasicHttpsBinding (BasicHttpsSecurityMode.TransportWithMessageCredential);
  144. exporter.ExportEndpoint (new ServiceEndpoint (
  145. cd, binding, new EndpointAddress (HttpsUri)));
  146. return exporter.GetGeneratedMetadata ();
  147. }
  148. #endif
  149. [MetadataSample]
  150. public static MetadataSet NetTcp ()
  151. {
  152. var exporter = new WsdlExporter ();
  153. var cd = new ContractDescription ("MyContract");
  154. exporter.ExportEndpoint (new ServiceEndpoint (
  155. cd, new NetTcpBinding (SecurityMode.None, false),
  156. new EndpointAddress (NetTcpUri)));
  157. return exporter.GetGeneratedMetadata ();
  158. }
  159. [MetadataSample]
  160. public static MetadataSet NetTcp_TransportSecurity ()
  161. {
  162. var exporter = new WsdlExporter ();
  163. var cd = new ContractDescription ("MyContract");
  164. exporter.ExportEndpoint (new ServiceEndpoint (
  165. cd, new NetTcpBinding (SecurityMode.Transport, false),
  166. new EndpointAddress (NetTcpUri)));
  167. return exporter.GetGeneratedMetadata ();
  168. }
  169. [MetadataSample]
  170. public static MetadataSet NetTcp_MessageSecurity ()
  171. {
  172. var exporter = new WsdlExporter ();
  173. var cd = new ContractDescription ("MyContract");
  174. exporter.ExportEndpoint (new ServiceEndpoint (
  175. cd, new NetTcpBinding (SecurityMode.Message, false),
  176. new EndpointAddress (NetTcpUri)));
  177. return exporter.GetGeneratedMetadata ();
  178. }
  179. [MetadataSample]
  180. public static MetadataSet NetTcp_TransportWithMessageCredential ()
  181. {
  182. var exporter = new WsdlExporter ();
  183. var cd = new ContractDescription ("MyContract");
  184. exporter.ExportEndpoint (new ServiceEndpoint (
  185. cd, new NetTcpBinding (SecurityMode.TransportWithMessageCredential, false),
  186. new EndpointAddress (NetTcpUri)));
  187. return exporter.GetGeneratedMetadata ();
  188. }
  189. [MetadataSample]
  190. public static MetadataSet NetTcp_ReliableSession ()
  191. {
  192. var exporter = new WsdlExporter ();
  193. var cd = new ContractDescription ("MyContract");
  194. var binding = new NetTcpBinding (SecurityMode.None, true);
  195. exporter.ExportEndpoint (new ServiceEndpoint (
  196. cd, binding, new EndpointAddress (NetTcpUri)));
  197. return exporter.GetGeneratedMetadata ();
  198. }
  199. [MetadataSample]
  200. public static MetadataSet NetTcp_TransferMode ()
  201. {
  202. var exporter = new WsdlExporter ();
  203. var cd = new ContractDescription ("MyContract");
  204. var binding = new NetTcpBinding (SecurityMode.None, false);
  205. binding.TransferMode = TransferMode.Streamed;
  206. exporter.ExportEndpoint (new ServiceEndpoint (
  207. cd, binding, new EndpointAddress (NetTcpUri)));
  208. return exporter.GetGeneratedMetadata ();
  209. }
  210. [ServiceContract]
  211. public interface IMyContract {
  212. [OperationContract]
  213. void Hello ();
  214. }
  215. [MetadataSample]
  216. public static MetadataSet BasicHttp_Operation ()
  217. {
  218. var exporter = new WsdlExporter ();
  219. var cd = ContractDescription.GetContract (typeof (IMyContract));
  220. var binding = new BasicHttpBinding ();
  221. exporter.ExportEndpoint (new ServiceEndpoint (
  222. cd, binding, new EndpointAddress (HttpUri)));
  223. return exporter.GetGeneratedMetadata ();
  224. }
  225. [MetadataSample]
  226. public static MetadataSet NetTcp_Operation ()
  227. {
  228. var exporter = new WsdlExporter ();
  229. var cd = ContractDescription.GetContract (typeof (IMyContract));
  230. exporter.ExportEndpoint (new ServiceEndpoint (
  231. cd, new NetTcpBinding (SecurityMode.None, false),
  232. new EndpointAddress (NetTcpUri)));
  233. return exporter.GetGeneratedMetadata ();
  234. }
  235. [MetadataSample (CreateConfig = true)]
  236. public static MetadataSet BasicHttp_Config ()
  237. {
  238. var exporter = new WsdlExporter ();
  239. var cd = ContractDescription.GetContract (typeof (IMyContract));
  240. var binding = new BasicHttpBinding ();
  241. exporter.ExportEndpoint (new ServiceEndpoint (
  242. cd, binding, new EndpointAddress (HttpUri)));
  243. return exporter.GetGeneratedMetadata ();
  244. }
  245. [MetadataSample (CreateConfig = true)]
  246. public static MetadataSet BasicHttp_Config2 ()
  247. {
  248. var exporter = new WsdlExporter ();
  249. var cd = ContractDescription.GetContract (typeof (IMyContract));
  250. exporter.ExportEndpoint (new ServiceEndpoint (
  251. cd, new BasicHttpBinding (),
  252. new EndpointAddress (HttpUri)));
  253. exporter.ExportEndpoint (new ServiceEndpoint (
  254. cd, new NetTcpBinding (SecurityMode.None, false),
  255. new EndpointAddress (NetTcpUri)));
  256. return exporter.GetGeneratedMetadata ();
  257. }
  258. #region Helper API
  259. public static void Export (string outputDir)
  260. {
  261. if (!Directory.Exists (outputDir))
  262. Directory.CreateDirectory (outputDir);
  263. var bf = BindingFlags.Public | BindingFlags.Static;
  264. foreach (var method in typeof (MetadataSamples).GetMethods (bf)) {
  265. MetadataSampleAttribute sampleAttr = null;
  266. foreach (var obj in method.GetCustomAttributes (false)) {
  267. var cattr = obj as MetadataSampleAttribute;
  268. if (cattr != null) {
  269. sampleAttr = cattr;
  270. break;
  271. }
  272. }
  273. if (sampleAttr == null)
  274. continue;
  275. var name = sampleAttr.Name ?? method.Name;
  276. var metadata = (MetadataSet)method.Invoke (null, null);
  277. var xmlFilename = Path.Combine (outputDir, name + ".xml");
  278. TestContext.SaveMetadata (xmlFilename, metadata);
  279. if (!sampleAttr.CreateConfig)
  280. continue;
  281. var configFilename = Path.Combine (outputDir, name + ".config");
  282. TestContext.GenerateConfig (configFilename, metadata);
  283. }
  284. }
  285. public static MetadataSet GetMetadataByName (string name)
  286. {
  287. if (name.EndsWith (".xml"))
  288. name = name.Substring (name.Length - 4);
  289. var bf = BindingFlags.Public | BindingFlags.Static;
  290. foreach (var method in typeof (MetadataSamples).GetMethods (bf)) {
  291. MetadataSampleAttribute sampleAttr = null;
  292. foreach (var obj in method.GetCustomAttributes (false)) {
  293. var cattr = obj as MetadataSampleAttribute;
  294. if (cattr != null) {
  295. sampleAttr = cattr;
  296. break;
  297. }
  298. }
  299. if (sampleAttr == null)
  300. continue;
  301. if (!name.Equals (sampleAttr.Name ?? method.Name))
  302. continue;
  303. return (MetadataSet)method.Invoke (null, null);
  304. }
  305. throw new InvalidOperationException (string.Format (
  306. "No such metadata sample: '{0}'", name));
  307. }
  308. public class MetadataSampleAttribute : Attribute {
  309. public MetadataSampleAttribute ()
  310. {
  311. }
  312. public MetadataSampleAttribute (string name)
  313. {
  314. Name = name;
  315. }
  316. public string Name {
  317. get; set;
  318. }
  319. public bool CreateConfig {
  320. get; set;
  321. }
  322. }
  323. #endregion
  324. }
  325. }