CallbackBehaviorAttributeTest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // CallbackBehaviorAttributeTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 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.ServiceModel.Dispatcher;
  37. using System.Transactions;
  38. using System.Threading;
  39. using NUnit.Framework;
  40. namespace MonoTests.System.ServiceModel
  41. {
  42. [TestFixture]
  43. public class CallbackBehaviorAttributeTest
  44. {
  45. [Test]
  46. public void DefaultValues ()
  47. {
  48. var c = new CallbackBehaviorAttribute ();
  49. Assert.IsTrue (c.AutomaticSessionShutdown, "#1");
  50. Assert.AreEqual (ConcurrencyMode.Single, c.ConcurrencyMode, "#2");
  51. Assert.IsFalse (c.IgnoreExtensionDataObject, "#3");
  52. Assert.IsFalse (c.IncludeExceptionDetailInFaults, "#4");
  53. Assert.AreEqual (0x10000, c.MaxItemsInObjectGraph, "#5");
  54. Assert.AreEqual (IsolationLevel.Unspecified, c.TransactionIsolationLevel, "#6");
  55. Assert.IsNull (c.TransactionTimeout, "#7");
  56. Assert.IsTrue (c.UseSynchronizationContext, "#8");
  57. Assert.IsTrue (c.ValidateMustUnderstand, "#9");
  58. }
  59. [Test]
  60. public void AddBindingParameters ()
  61. {
  62. IEndpointBehavior eb = new CallbackBehaviorAttribute ();
  63. var cd = ContractDescription.GetContract (typeof (IFoo));
  64. var se = new ServiceEndpoint (cd);
  65. Assert.AreEqual (0, se.Behaviors.Count, "#1");
  66. var pc = new BindingParameterCollection ();
  67. eb.AddBindingParameters (se, pc);
  68. Assert.AreEqual (0, pc.Count, "#2");
  69. Assert.AreEqual (0, se.Behaviors.Count, "#3");
  70. }
  71. [Test]
  72. [ExpectedException (typeof (InvalidOperationException))]
  73. public void ApplyDispatchBehavior ()
  74. {
  75. // It cannot be applied to service endpoint dispatcher.
  76. IEndpointBehavior eb = new CallbackBehaviorAttribute () { ConcurrencyMode = ConcurrencyMode.Multiple, ValidateMustUnderstand = false };
  77. var cd = ContractDescription.GetContract (typeof (IFoo));
  78. var se = new ServiceEndpoint (cd);
  79. var ed = new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "IFoo", "urn:foo");
  80. eb.ApplyDispatchBehavior (se, ed);
  81. }
  82. [Test]
  83. [ExpectedException (typeof (InvalidOperationException))]
  84. public void ApplyClientBehaviorNonDuplex ()
  85. {
  86. // It must be applied to duplex callback runtime
  87. IEndpointBehavior eb = new CallbackBehaviorAttribute () { ConcurrencyMode = ConcurrencyMode.Multiple, ValidateMustUnderstand = false };
  88. var cd = ContractDescription.GetContract (typeof (IFoo));
  89. var se = new ServiceEndpoint (cd);
  90. var ed = new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "IFoo", "urn:foo");
  91. var cr = ed.DispatchRuntime.CallbackClientRuntime;
  92. eb.ApplyClientBehavior (se, cr);
  93. }
  94. /* There is no way that I can create ClientRuntime instance ...
  95. [Test]
  96. public void ApplyClientBehavior ()
  97. {
  98. IEndpointBehavior eb = new CallbackBehaviorAttribute () { ConcurrencyMode = ConcurrencyMode.Multiple, ValidateMustUnderstand = false };
  99. var cd = ContractDescription.GetContract (typeof (IDuplexFoo));
  100. var se = new ServiceEndpoint (cd);
  101. var ed = new EndpointDispatcher (new EndpointAddress ("http://localhost:37564"), "IDuplexFoo", "urn:foo");
  102. var cr = ed.DispatchRuntime.CallbackClientRuntime;
  103. eb.ApplyClientBehavior (se, cr);
  104. Assert.IsFalse (cr.ValidateMustUnderstand, "#2.1");
  105. //Assert.IsFalse (cr.CallbackDispatchRuntime.ValidateMustUnderstand, "#2.2");
  106. Assert.AreEqual (1, se.Behaviors.Count, "#3");
  107. }
  108. */
  109. [ServiceContract]
  110. public interface IFoo
  111. {
  112. [OperationContract]
  113. string Join (string s1, string s2);
  114. }
  115. [ServiceContract (CallbackContract = typeof (IFoo))]
  116. public interface IDuplexFoo
  117. {
  118. [OperationContract]
  119. void Block (string s);
  120. }
  121. #region "bug #567672"
  122. [Test]
  123. public void CallbackExample1 ()
  124. {
  125. //Start service and use net.tcp binding
  126. ServiceHost eventServiceHost = new ServiceHost (typeof (GreetingsService));
  127. NetTcpBinding tcpBindingpublish = new NetTcpBinding ();
  128. tcpBindingpublish.Security.Mode = SecurityMode.None;
  129. eventServiceHost.AddServiceEndpoint (typeof (IGreetings), tcpBindingpublish, "net.tcp://localhost:8000/GreetingsService");
  130. eventServiceHost.Open ();
  131. //Create client proxy
  132. NetTcpBinding clientBinding = new NetTcpBinding ();
  133. clientBinding.Security.Mode = SecurityMode.None;
  134. EndpointAddress ep = new EndpointAddress ("net.tcp://localhost:8000/GreetingsService");
  135. ClientCallback cb = new ClientCallback ();
  136. IGreetings proxy = DuplexChannelFactory<IGreetings>.CreateChannel (new InstanceContext (cb), clientBinding, ep);
  137. //Call service
  138. proxy.SendMessage ();
  139. //Wait for callback - sort of hack, but better than using wait handle to possibly block tests.
  140. Thread.Sleep (1000);
  141. //Cleanup
  142. eventServiceHost.Close ();
  143. Assert.IsTrue (CallbackSent, "#1");
  144. Assert.IsTrue (CallbackReceived, "#1");
  145. }
  146. public static bool CallbackSent, CallbackReceived;
  147. //Service implementation
  148. [ServiceBehavior (ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
  149. public class GreetingsService : IGreetings
  150. {
  151. public void SendMessage ()
  152. {
  153. //Make a callback
  154. IGreetingsCallback clientCallback = OperationContext.Current.GetCallbackChannel<IGreetingsCallback> ();
  155. clientCallback.ShowMessage ("Mono and WCF are GREAT!");
  156. CallbackBehaviorAttributeTest.CallbackSent = true;
  157. }
  158. }
  159. // Client callback interface implementation
  160. [CallbackBehavior (ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
  161. public class ClientCallback : IGreetingsCallback
  162. {
  163. public void ShowMessage (string message)
  164. {
  165. CallbackBehaviorAttributeTest.CallbackReceived = true;
  166. }
  167. }
  168. [ServiceContract (CallbackContract = typeof (IGreetingsCallback))]
  169. public interface IGreetings
  170. {
  171. [OperationContract (IsOneWay = true)]
  172. void SendMessage ();
  173. }
  174. [ServiceContract]
  175. public interface IGreetingsCallback
  176. {
  177. [OperationContract (IsOneWay = true)]
  178. void ShowMessage (string message);
  179. }
  180. #endregion
  181. }
  182. }