[runtime] Fixed get_process_module module name.
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / IntegratedConnectionTest.cs
1 //
2 // IntegratedConnectionTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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;
32 using System.Net.Security;
33 using System.Runtime.Serialization;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
37 using System.ServiceModel.Dispatcher;
38 using System.Threading;
39 using System.Xml;
40 using NUnit.Framework;
41
42
43 namespace MonoTests.System.ServiceModel
44 {
45         // FIXME: uncomment when NUnit tests on HTTP connection got as
46         // stable as non-nunit samples.
47         
48         [TestFixture]
49         public class IntegratedConnectionTest
50         {
51                 [Test]
52                 [Ignore ("With Orcas it does not work fine")]
53                 // It is almost identical to samples/basic-http-binding
54                 public void SimpleHttpConnection ()
55                 {
56                         // Service
57                         ServiceHost host = new ServiceHost (typeof (Foo));
58                         try {
59                                 Binding binding = new BasicHttpBinding ();
60                                 binding.SendTimeout = binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
61                                 ServiceEndpoint se = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.IFoo",
62                                         binding, new Uri ("http://localhost:37564"));
63                                 host.Open ();
64
65                                 // Client
66                                 ChannelFactory<IFoo> cf = new ChannelFactory<IFoo> (
67                                         new BasicHttpBinding (),
68                                         new EndpointAddress ("http://localhost:37564/"));
69                                 IFoo foo  = cf.CreateChannel ();
70                                 Assert.AreEqual ("Test for EchoTest for Echo", foo.Echo ("Test for Echo"));
71                         } finally {
72                                 if (host.State == CommunicationState.Opened)
73                                         host.Close ();
74                         }
75                 }
76
77                 [Test]
78                 [Ignore ("With Orcas it does not work fine")]
79                 public void SimpleClientBase ()
80                 {
81                         // Service
82                         ServiceHost host = new ServiceHost (typeof (Foo2));
83                         try {
84                                 Binding binding = new BasicHttpBinding ();
85                                 binding.SendTimeout = binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
86                                 host.AddServiceEndpoint ("MonoTests.System.ServiceModel.IFoo2",
87                                 binding, new Uri ("http://localhost:37564"));
88                                 host.Open ();
89
90                                 // Client
91                                 Foo2Proxy proxy = new Foo2Proxy (
92                                         new BasicHttpBinding (),
93                                         new EndpointAddress ("http://localhost:37564/"));
94                                 proxy.Open ();
95                                 try {
96                                         Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO",
97                                                 proxy.Echo ("TEST FOR ECHO"));
98                                 } finally {
99                                         proxy.Close ();
100                                 }
101                         } finally {
102                                 // Service
103                                 if (host.State == CommunicationState.Opened)
104                                         host.Close ();
105                         }
106                 }
107
108                 [Test]
109                 [Ignore ("With Orcas it does not work fine")]
110                 public void ExchangeMetadata ()
111                 {
112                         // Service
113                         ServiceHost host = new ServiceHost (typeof (MetadataExchange));
114                         try {
115                                 Binding binding = new BasicHttpBinding ();
116                                 binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
117                                 host.AddServiceEndpoint ("IMetadataExchange",
118                                 binding, new Uri ("http://localhost:37564"));
119                                 host.Open ();
120                                 // Client
121
122                                 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
123                                         new BasicHttpBinding (),
124                                         new EndpointAddress ("http://localhost:37564/"));
125                                 proxy.Open ();
126
127                                 try {
128                                         Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
129                                         Message res = proxy.Get (req);
130                                 } finally {
131                                         proxy.Close ();
132                                 }
133                         } finally {
134                                 // Service
135                                 if (host.State == CommunicationState.Opened)
136                                         host.Close ();
137                         }
138                 }
139
140                 [ServiceContract(SessionMode = SessionMode.Required)]
141                 interface IFoo3
142                 {
143                         [OperationContract]
144                         int GetInstanceCounter ();
145                 }
146
147                         [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)]
148                 class Foo3 : IFoo3, IDisposable
149                 {
150                         public static int CreatedInstances { get; set; }
151                         public static int DisposedInstances { get; set; }
152
153                         public static int InstanceCounter {
154                                 get { return CreatedInstances - DisposedInstances; }
155                         }
156
157                         public Foo3 ()
158                         {
159                                 CreatedInstances++;
160                         }
161
162                         public int GetInstanceCounter ()
163                         {
164                                 return InstanceCounter;
165                         }
166
167                         public virtual void Dispose ()
168                         {
169                                 DisposedInstances++;
170                         }
171
172                 }
173
174                 private void TestSessionbehaviour (Binding binding, Uri address)
175                 {
176                         ServiceHost host = new ServiceHost (typeof (Foo3), address);
177                         host.AddServiceEndpoint (typeof (IFoo3), binding, address);
178                         host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentSessions = 1 });
179                         Foo3.CreatedInstances = Foo3.DisposedInstances = 0; // Reset
180                         host.Open ();
181                         Assert.AreEqual (0, Foo3.InstanceCounter, "Initial state wrong"); // just to be sure
182                         var cd = (ChannelDispatcher) host.ChannelDispatchers [0];
183                         for (int i = 1; i <= 3; ++i) {
184                                 var factory = new ChannelFactory<IFoo3Client> (binding, address.ToString ());
185                                 var proxy = factory.CreateChannel ();
186                                 Assert.AreEqual (1, proxy.GetInstanceCounter (), "One server instance after first call in session #" + i);
187                                 Assert.AreEqual (1, proxy.GetInstanceCounter (), "One server instance after second call in session #" + i);
188                                 factory.Close (); // should close session even when no IsTerminating method has been invoked
189                                 Thread.Sleep (500); // give WCF time to dispose service object
190                                 Assert.AreEqual (0, Foo3.InstanceCounter, "Service instances must be disposed after channel is closed, in session #" + i);
191                                 Assert.AreEqual (i, Foo3.CreatedInstances, "One new instance per session, in session #" + i);
192                         }
193                         host.Close ();
194                 }
195
196                 interface IFoo3Client : IFoo3, IClientChannel {}
197
198                 [Test (Description = "Tests InstanceContextMode.PerSession behavior for NetTcp binding")]
199                 public void TestSessionInstancesNetTcp ()
200                 {
201                         Binding binding = new NetTcpBinding (SecurityMode.None, false);
202                         Uri address = new Uri (binding.Scheme + "://localhost:9999/test");
203                         TestSessionbehaviour (binding, address);
204                 }
205
206                 [Test (Description = "Tests InstanceContextMode.PerSession behavior for WsHttp binding")]
207                 [Category ("NotWorking")] // no working WSHttpBinding in mono.
208                 public void TestSessionInstancesWsHttp ()
209                 {
210                         Binding binding = new WSHttpBinding (SecurityMode.None, true);
211                         Uri address = new Uri (binding.Scheme + "://localhost:9999/test");
212                         TestSessionbehaviour(binding, address);
213                 }
214         }
215
216         #region SimpleConnection classes
217
218         [ServiceContract]
219         public interface IFoo
220         {
221                 [OperationContract]
222                 string Echo (string msg);
223         }
224
225         class Foo : IFoo
226         {
227                 public string Echo (string msg)
228                 {
229                         return msg + msg;
230                 }
231         }
232
233         // This is manually created type for strongly typed request.
234         [DataContract (Name = "Echo", Namespace = "http://tempuri.org/")]
235         public class EchoType
236         {
237                 public EchoType (string msg)
238                 {
239                         this.msg = msg;
240                 }
241
242                 [DataMember]
243                 public string msg = "test";
244         }
245
246         #endregion
247
248         #region SampleClientBase classes
249
250         [ServiceContract]
251         public interface IFoo2
252         {
253                 [OperationContract]
254                 string Echo (string msg);
255         }
256
257         class Foo2 : IFoo2
258         {
259                 public string Echo (string msg)
260                 {
261                         return msg + msg;
262                 }
263         }
264
265         public class Foo2Proxy : ClientBase<IFoo2>, IFoo2
266         {
267                 public Foo2Proxy (Binding binding, EndpointAddress address)
268                         : base (binding, address)
269                 {
270                 }
271
272                 public string Echo (string msg)
273                 {
274                         return Channel.Echo (msg);
275                 }
276         }
277
278         #endregion
279
280         #region ExchangeMetadata classes
281
282         class MetadataExchange : IMetadataExchange
283         {
284                 public Message Get (Message request)
285                 {
286                         XmlDocument doc = new XmlDocument ();
287                         doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
288                         return Message.CreateMessage (request.Version,
289                                 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
290                                 new XmlNodeReader (doc));
291                 }
292
293                 public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
294                 {
295                         throw new NotImplementedException ();
296                 }
297
298                 public Message EndGet (IAsyncResult result)
299                 {
300                         throw new NotImplementedException ();
301                 }
302         }
303
304         public class MetadataExchangeProxy : ClientBase<IMetadataExchange>, IMetadataExchange
305         {
306                 public MetadataExchangeProxy (Binding binding, EndpointAddress address)
307                         : base (binding, address)
308                 {
309                 }
310
311                 public Message Get (Message request)
312                 {
313                         return Channel.Get (request);
314                 }
315
316                 public IAsyncResult BeginGet (Message request, AsyncCallback callback, object state)
317                 {
318                         throw new NotImplementedException ();
319                 }
320
321                 public Message EndGet (IAsyncResult result)
322                 {
323                         throw new NotImplementedException ();
324                 }
325         }
326
327         #endregion
328 }