NetTcpBindingTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // NetTcpBindingTest.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.Net.Sockets;
  31. using System.Net.Security;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Security;
  35. using System.Threading;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.ServiceModel
  38. {
  39. [TestFixture]
  40. public class NetTcpBindingTest
  41. {
  42. [Test]
  43. public void DefaultValues ()
  44. {
  45. var n = new NetTcpBinding ();
  46. Assert.AreEqual (HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
  47. Assert.AreEqual (10, n.ListenBacklog, "#2");
  48. Assert.AreEqual (false, n.PortSharingEnabled, "#3");
  49. var tr = n.CreateBindingElements ().Find<TcpTransportBindingElement> ();
  50. Assert.IsNotNull (tr, "#tr1");
  51. Assert.AreEqual (false, tr.TeredoEnabled, "#tr2");
  52. Assert.AreEqual ("net.tcp", tr.Scheme, "#tr3");
  53. Assert.IsFalse (n.TransactionFlow, "#4");
  54. var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
  55. Assert.IsNotNull (tx, "#tx1");
  56. }
  57. [Test]
  58. public void BufferedConnection ()
  59. {
  60. var host = new ServiceHost (typeof (Foo));
  61. var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement ());
  62. host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
  63. host.Open (TimeSpan.FromSeconds (5));
  64. try {
  65. var bindingcli = new NetTcpBinding () { TransactionFlow = false };
  66. bindingcli.Security.Mode = SecurityMode.None;
  67. var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
  68. Assert.AreEqual (5, cli.Add (1, 4));
  69. Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
  70. } finally {
  71. host.Close (TimeSpan.FromSeconds (5));
  72. var t = new TcpListener (37564);
  73. t.Start ();
  74. t.Stop ();
  75. }
  76. Assert.IsTrue (Foo.AddCalled, "#1");
  77. Assert.IsTrue (Foo.JoinCalled, "#2");
  78. }
  79. [Test]
  80. public void StreamedConnection ()
  81. {
  82. var host = new ServiceHost (typeof (Foo));
  83. var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement () { TransferMode = TransferMode.Streamed });
  84. host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
  85. host.Open (TimeSpan.FromSeconds (5));
  86. try {
  87. var bindingcli = new NetTcpBinding () { TransactionFlow = false };
  88. bindingcli.TransferMode = TransferMode.Streamed;
  89. bindingcli.Security.Mode = SecurityMode.None;
  90. var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
  91. Assert.AreEqual (5, cli.Add (1, 4));
  92. Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
  93. } finally {
  94. host.Close (TimeSpan.FromSeconds (5));
  95. var t = new TcpListener (37564);
  96. t.Start ();
  97. t.Stop ();
  98. }
  99. Assert.IsTrue (Foo.AddCalled, "#1");
  100. Assert.IsTrue (Foo.JoinCalled, "#2");
  101. }
  102. [ServiceContract]
  103. public interface IFoo
  104. {
  105. [OperationContract]
  106. int Add (short s, int i);
  107. [OperationContract]
  108. string Join (string s1, string s2);
  109. }
  110. public interface IFooClient : IFoo, IClientChannel
  111. {
  112. }
  113. public class Foo : IFoo
  114. {
  115. public static bool AddCalled;
  116. public static bool JoinCalled;
  117. public int Add (short s, int i)
  118. {
  119. AddCalled = true;
  120. return s + i;
  121. }
  122. public string Join (string s1, string s2)
  123. {
  124. JoinCalled = true;
  125. return s1 + " " + s2;
  126. }
  127. }
  128. }
  129. }