ClientBase_InteractiveChannelInitializerTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // ClientBase_InteractiveChannelInitializerTest.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.ServiceModel.Dispatcher;
  37. using System.Xml;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.ServiceModel
  40. {
  41. [TestFixture]
  42. // FIXME: it does not contain testcase for successful initialization yet
  43. // (MyChannelInitializer implementation hangs on .NET)
  44. public class ClientBase_InteractiveChannelInitializerTest
  45. {
  46. [Test]
  47. [ExpectedException (typeof (InvalidOperationException))]
  48. public void NotAllowedInitializationUI ()
  49. {
  50. var f = new FooProxy (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564"));
  51. f.Endpoint.Contract.Behaviors.Add (new MyContractBehavior ());
  52. f.InnerChannel.AllowInitializationUI = false;
  53. f.DisplayInitializationUI ();
  54. }
  55. [Test]
  56. [ExpectedException (typeof (InvalidOperationException))]
  57. public void OpenBeforeDisplayInitializationUI ()
  58. {
  59. var f = new FooProxy (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564"));
  60. f.Endpoint.Contract.Behaviors.Add (new MyContractBehavior ());
  61. f.Open ();
  62. }
  63. public class MyContractBehavior2 : MyContractBehavior
  64. {
  65. }
  66. public class MyContractBehavior : IContractBehavior
  67. {
  68. public void AddBindingParameters (ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  69. {
  70. }
  71. public void ApplyClientBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  72. {
  73. clientRuntime.InteractiveChannelInitializers.Add (new MyChannelInitializer ());
  74. }
  75. public void ApplyDispatchBehavior (ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
  76. {
  77. }
  78. public void Validate (ContractDescription contractDescription, ServiceEndpoint endpoint)
  79. {
  80. }
  81. }
  82. public class MyChannelInitializer : IInteractiveChannelInitializer
  83. {
  84. delegate void DoWork ();
  85. DoWork d;
  86. static int instance;
  87. int number;
  88. public MyChannelInitializer ()
  89. {
  90. number = instance++;
  91. }
  92. public IAsyncResult BeginDisplayInitializationUI (IClientChannel channel, AsyncCallback callback, object state)
  93. {
  94. Console.WriteLine ("Begin");
  95. d = new DoWork (DisplayInitializationUI);
  96. return d.BeginInvoke (null, null);
  97. }
  98. public void EndDisplayInitializationUI (IAsyncResult result)
  99. {
  100. Console.WriteLine ("End");
  101. d.EndInvoke (result);
  102. }
  103. public void DisplayInitializationUI ()
  104. {
  105. Console.WriteLine ("Core: " + number);
  106. }
  107. }
  108. public class FooProxy : ClientBase<IFoo>, IFoo
  109. {
  110. public FooProxy (Binding binding, EndpointAddress address)
  111. : base (binding, address)
  112. {
  113. }
  114. public string Echo (string msg)
  115. {
  116. return Channel.Echo (msg);
  117. }
  118. }
  119. [ServiceContract]
  120. public interface IFoo
  121. {
  122. [OperationContract]
  123. string Echo (string input);
  124. }
  125. public class FooService : IFoo
  126. {
  127. public string Echo (string input)
  128. {
  129. return input;
  130. }
  131. }
  132. }
  133. }