ClientBaseTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. //
  2. // ClientBaseTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 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.Collections.ObjectModel;
  30. using System.IO;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Channels;
  35. using System.ServiceModel.Description;
  36. using System.Xml;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.ServiceModel
  39. {
  40. [TestFixture]
  41. public class ClientBaseTest
  42. {
  43. /*
  44. [Test]
  45. [ExpectedException (typeof (InvalidOperationException))]
  46. public void GenericTypeArgumentIsServiceContract ()
  47. {
  48. new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
  49. }
  50. */
  51. /*
  52. public class MyClientBase<T> : ClientBase<T>
  53. {
  54. public MyClientBase (Binding binding, EndpointAddress address)
  55. : base (binding, address)
  56. {
  57. }
  58. }
  59. public class MyClientBase1 : ClientBase<TestService>
  60. {
  61. public MyClientBase1 (Binding binding, EndpointAddress address)
  62. : base (binding, address)
  63. {
  64. }
  65. }
  66. [Test]
  67. public void ClassTypeArg ()
  68. {
  69. new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
  70. }
  71. */
  72. [ServiceContract]
  73. public interface ITestService
  74. {
  75. [OperationContract]
  76. string Foo (string arg);
  77. }
  78. public class TestService : ITestService
  79. {
  80. public string Foo (string arg)
  81. {
  82. return arg;
  83. }
  84. }
  85. [ServiceContract]
  86. public interface ITestService2
  87. {
  88. [OperationContract]
  89. void Bar (string arg);
  90. }
  91. public class TestService2 : ITestService2
  92. {
  93. public void Bar (string arg)
  94. {
  95. }
  96. }
  97. [Test]
  98. [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
  99. public void Foo ()
  100. {
  101. Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
  102. Assert.IsTrue (t.IsGenericParameter);
  103. Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
  104. }
  105. class MyChannelFactory<T> : ChannelFactory<T>
  106. {
  107. public MyChannelFactory (Binding b, EndpointAddress e)
  108. : base (b, e)
  109. {
  110. }
  111. public IChannelFactory GimmeFactory ()
  112. {
  113. return CreateFactory ();
  114. }
  115. }
  116. #region UseCase1
  117. ServiceHost host;
  118. [Test]
  119. [ExpectedException (typeof (ArgumentNullException))]
  120. public void ClientBaseCtorArgsTest1 ()
  121. {
  122. new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentNullException))]
  126. public void ClientBaseCtorArgsTest2 ()
  127. {
  128. new CtorUseCase1 (null, new EndpointAddress ("http://test"));
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentNullException))]
  132. public void ClientBaseCtorArgsTest3 ()
  133. {
  134. new CtorUseCase1 (null, "http://test");
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentNullException))]
  138. public void ClientBaseCtorArgsTest4 ()
  139. {
  140. new CtorUseCase1 ("CtorUseCase1_1", null);
  141. }
  142. [Test]
  143. [ExpectedException (typeof (ArgumentNullException))]
  144. public void ClientBaseCtorArgsTest5 ()
  145. {
  146. new CtorUseCase1 (new BasicHttpBinding (), null);
  147. }
  148. [Test]
  149. [ExpectedException (typeof (InvalidOperationException))]
  150. public void ClientBaseCtorArgsTest6 ()
  151. {
  152. new CtorUseCase1 ("CtorUseCase1_Incorrect");
  153. }
  154. [Test]
  155. [ExpectedException (typeof (InvalidOperationException))]
  156. public void ClientBaseCtorArgsTest7 ()
  157. {
  158. new CtorUseCase3 ();
  159. }
  160. [Test]
  161. [ExpectedException (typeof (InvalidOperationException))]
  162. public void ClientBaseCtorConfigAmbiguityTest ()
  163. {
  164. new CtorUseCase2 ();
  165. }
  166. [Test]
  167. [ExpectedException (typeof (InvalidOperationException))]
  168. public void ClientBaseCtorConfigAmbiguityTest2 ()
  169. {
  170. new CtorUseCase2 ("*");
  171. }
  172. [Test]
  173. [Ignore ("fails under .NET; I never bothered to fix the test")]
  174. public void ClientBaseConfigEmptyCtor ()
  175. {
  176. new CtorUseCase1 ();
  177. }
  178. [Test]
  179. [Ignore ("fails under .NET; I never bothered to fix the test")]
  180. public void ClientBaseConfigCtor ()
  181. {
  182. new CtorUseCase1 ("CtorUseCase1_1");
  183. }
  184. [Test]
  185. [Ignore ("fails under .NET; I never bothered to fix the test")]
  186. public void ClientBaseConfigCtorUsingDefault ()
  187. {
  188. new CtorUseCase1 ("*");
  189. }
  190. [Test]
  191. [Ignore ("With Orcas it does not work fine")]
  192. public void UseCase1Test ()
  193. {
  194. // almost equivalent to samples/clientbase/samplesvc.cs
  195. using (host = new ServiceHost (typeof (UseCase1))) {
  196. Binding binding = new BasicHttpBinding ();
  197. binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
  198. host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:37564"));
  199. host.Open ();
  200. // almost equivalent to samples/clientbase/samplecli.cs
  201. using (UseCase1Proxy proxy = new UseCase1Proxy (
  202. new BasicHttpBinding (),
  203. new EndpointAddress ("http://localhost:37564"))) {
  204. proxy.Open ();
  205. Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
  206. }
  207. }
  208. EnsurePortNonBlocking (37564);
  209. }
  210. void EnsurePortNonBlocking (int port)
  211. {
  212. TcpListener l = new TcpListener (port);
  213. l.ExclusiveAddressUse = true;
  214. l.Start ();
  215. l.Stop ();
  216. }
  217. [ServiceContract]
  218. public interface IUseCase1
  219. {
  220. [OperationContract]
  221. string Echo (string msg);
  222. }
  223. public class UseCase1 : IUseCase1
  224. {
  225. public string Echo (string msg)
  226. {
  227. return msg + msg;
  228. }
  229. }
  230. public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
  231. {
  232. public UseCase1Proxy (Binding binding, EndpointAddress address)
  233. : base (binding, address)
  234. {
  235. }
  236. public string Echo (string msg)
  237. {
  238. return Channel.Echo (msg);
  239. }
  240. }
  241. public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
  242. {
  243. public CtorUseCase1 ()
  244. : base ()
  245. {
  246. }
  247. public CtorUseCase1 (string configName)
  248. : base (configName)
  249. {
  250. }
  251. public CtorUseCase1 (string configName, string address)
  252. : base (configName, address)
  253. {
  254. }
  255. public CtorUseCase1 (Binding binding, EndpointAddress address)
  256. : base (binding, address)
  257. {
  258. }
  259. public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
  260. : base (i, binding, address)
  261. {
  262. }
  263. public string Echo (string msg)
  264. {
  265. return Channel.Echo (msg);
  266. }
  267. }
  268. public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
  269. {
  270. public CtorUseCase2 ()
  271. : base ()
  272. {
  273. }
  274. public CtorUseCase2 (string configName)
  275. : base (configName)
  276. {
  277. }
  278. public CtorUseCase2 (string configName, string address)
  279. : base (configName, address)
  280. {
  281. }
  282. public CtorUseCase2 (Binding binding, EndpointAddress address)
  283. : base (binding, address)
  284. {
  285. }
  286. public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
  287. : base (i, binding, address)
  288. {
  289. }
  290. public string Echo (string msg)
  291. {
  292. return Channel.Echo (msg);
  293. }
  294. }
  295. public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
  296. {
  297. public string Echo (string msg)
  298. {
  299. return Channel.Echo (msg);
  300. }
  301. }
  302. #endregion
  303. // For contract that directly receives and sends Message instances.
  304. #region UseCase2
  305. [Test]
  306. [Ignore ("With Orcas it does not work fine")]
  307. public void UseCase2Test ()
  308. {
  309. // almost equivalent to samples/clientbase/samplesvc2.cs
  310. ServiceHost host = new ServiceHost (typeof (UseCase2));
  311. Binding binding = new BasicHttpBinding ();
  312. binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
  313. host.AddServiceEndpoint (typeof (IUseCase2).FullName,
  314. binding, new Uri ("http://localhost:37564"));
  315. try {
  316. host.Open ();
  317. // almost equivalent to samples/clientbase/samplecli2.cs
  318. Binding b = new BasicHttpBinding ();
  319. b.SendTimeout = TimeSpan.FromSeconds (15);
  320. b.ReceiveTimeout = TimeSpan.FromSeconds (15);
  321. UseCase2Proxy proxy = new UseCase2Proxy (
  322. b,
  323. new EndpointAddress ("http://localhost:37564/"));
  324. proxy.Open ();
  325. Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
  326. Message res = proxy.Echo (req);
  327. using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
  328. res.WriteMessage (w);
  329. }
  330. } finally {
  331. if (host.State == CommunicationState.Opened)
  332. host.Close ();
  333. EnsurePortNonBlocking (37564);
  334. }
  335. }
  336. [ServiceContract]
  337. public interface IUseCase2
  338. {
  339. [OperationContract]
  340. Message Echo (Message request);
  341. }
  342. class UseCase2 : IUseCase2
  343. {
  344. public Message Echo (Message request)
  345. {
  346. Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
  347. msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
  348. //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
  349. return msg;
  350. }
  351. }
  352. public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
  353. {
  354. public UseCase2Proxy (Binding binding, EndpointAddress address)
  355. : base (binding, address)
  356. {
  357. }
  358. public Message Echo (Message request)
  359. {
  360. return Channel.Echo (request);
  361. }
  362. }
  363. #endregion
  364. [Test]
  365. [Ignore ("With Orcas it does not work fine")]
  366. public void UseCase3 ()
  367. {
  368. // almost equivalent to samples/clientbase/samplesvc3.cs
  369. ServiceHost host = new ServiceHost (typeof (MetadataExchange));
  370. host.Description.Behaviors.Find<ServiceDebugBehavior> ()
  371. .IncludeExceptionDetailInFaults = true;
  372. Binding bs = new BasicHttpBinding ();
  373. bs.SendTimeout = TimeSpan.FromSeconds (5);
  374. bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
  375. // magic name that does not require fully qualified name ...
  376. host.AddServiceEndpoint ("IMetadataExchange",
  377. bs, new Uri ("http://localhost:37564"));
  378. try {
  379. host.Open ();
  380. // almost equivalent to samples/clientbase/samplecli3.cs
  381. Binding bc = new BasicHttpBinding ();
  382. bc.SendTimeout = TimeSpan.FromSeconds (5);
  383. bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
  384. MetadataExchangeProxy proxy = new MetadataExchangeProxy (
  385. bc,
  386. new EndpointAddress ("http://localhost:37564/"));
  387. proxy.Open ();
  388. Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
  389. Message res = proxy.Get (req);
  390. using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
  391. res.WriteMessage (w);
  392. }
  393. } finally {
  394. if (host.State == CommunicationState.Opened)
  395. host.Close ();
  396. EnsurePortNonBlocking (37564);
  397. }
  398. }
  399. class MetadataExchange : IMetadataExchange
  400. {
  401. public Message Get (Message request)
  402. {
  403. XmlDocument doc = new XmlDocument ();
  404. doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
  405. return Message.CreateMessage (request.Version,
  406. "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
  407. new XmlNodeReader (doc));
  408. }
  409. public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
  410. {
  411. throw new NotImplementedException ();
  412. }
  413. public Message EndGet (IAsyncResult result)
  414. {
  415. throw new NotImplementedException ();
  416. }
  417. }
  418. }
  419. }