5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.ObjectModel;
31 using System.Net.Sockets;
32 using System.Reflection;
33 using System.ServiceModel;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
37 using NUnit.Framework;
39 namespace MonoTests.System.ServiceModel
42 public class ClientBaseTest
46 [ExpectedException (typeof (InvalidOperationException))]
47 public void GenericTypeArgumentIsServiceContract ()
49 new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
54 public class MyClientBase<T> : ClientBase<T>
56 public MyClientBase (Binding binding, EndpointAddress address)
57 : base (binding, address)
62 public class MyClientBase1 : ClientBase<TestService>
64 public MyClientBase1 (Binding binding, EndpointAddress address)
65 : base (binding, address)
71 public void ClassTypeArg ()
73 new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
78 public interface ITestService
81 string Foo (string arg);
84 public class TestService : ITestService
86 public string Foo (string arg)
93 public interface ITestService2
96 void Bar (string arg);
99 public class TestService2 : ITestService2
101 public void Bar (string arg)
107 [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
110 Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
111 Assert.IsTrue (t.IsGenericParameter);
112 Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
115 class MyChannelFactory<T> : ChannelFactory<T>
117 public MyChannelFactory (Binding b, EndpointAddress e)
122 public IChannelFactory GimmeFactory ()
124 return CreateFactory ();
133 [ExpectedException (typeof (ArgumentNullException))]
134 public void ClientBaseCtorArgsTest1 ()
136 new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
140 [ExpectedException (typeof (ArgumentNullException))]
141 public void ClientBaseCtorArgsTest2 ()
143 new CtorUseCase1 (null, new EndpointAddress ("http://test"));
147 [ExpectedException (typeof (ArgumentNullException))]
148 public void ClientBaseCtorArgsTest3 ()
150 new CtorUseCase1 (null, "http://test");
154 [ExpectedException (typeof (ArgumentNullException))]
155 public void ClientBaseCtorArgsTest4 ()
157 new CtorUseCase1 ("CtorUseCase1_1", null);
161 [ExpectedException (typeof (ArgumentNullException))]
162 public void ClientBaseCtorArgsTest5 ()
164 new CtorUseCase1 (new BasicHttpBinding (), null);
168 [ExpectedException (typeof (InvalidOperationException))]
169 public void ClientBaseCtorArgsTest6 ()
171 new CtorUseCase1 ("CtorUseCase1_Incorrect");
175 [ExpectedException (typeof (InvalidOperationException))]
176 public void ClientBaseCtorArgsTest7 ()
182 [ExpectedException (typeof (InvalidOperationException))]
183 public void ClientBaseCtorConfigAmbiguityTest ()
189 [ExpectedException (typeof (InvalidOperationException))]
190 public void ClientBaseCtorConfigAmbiguityTest2 ()
192 new CtorUseCase2 ("*");
196 [Ignore ("fails under .NET; I never bothered to fix the test")]
197 public void ClientBaseConfigEmptyCtor ()
203 [Ignore ("fails under .NET; I never bothered to fix the test")]
204 public void ClientBaseConfigCtor ()
206 new CtorUseCase1 ("CtorUseCase1_1");
210 [Ignore ("fails under .NET; I never bothered to fix the test")]
211 public void ClientBaseConfigCtorUsingDefault ()
213 new CtorUseCase1 ("*");
217 [Ignore ("With Orcas it does not work fine")]
218 public void UseCase1Test ()
220 // almost equivalent to samples/clientbase/samplesvc.cs
221 using (host = new ServiceHost (typeof (UseCase1))) {
222 Binding binding = new BasicHttpBinding ();
223 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
224 host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:37564"));
227 // almost equivalent to samples/clientbase/samplecli.cs
228 using (UseCase1Proxy proxy = new UseCase1Proxy (
229 new BasicHttpBinding (),
230 new EndpointAddress ("http://localhost:37564"))) {
232 Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
235 EnsurePortNonBlocking (37564);
238 void EnsurePortNonBlocking (int port)
240 TcpListener l = new TcpListener (port);
241 l.ExclusiveAddressUse = true;
247 public interface IUseCase1
250 string Echo (string msg);
253 public class UseCase1 : IUseCase1
255 public string Echo (string msg)
261 public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
263 public UseCase1Proxy (Binding binding, EndpointAddress address)
264 : base (binding, address)
268 public string Echo (string msg)
270 return Channel.Echo (msg);
274 public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
276 public CtorUseCase1 ()
281 public CtorUseCase1 (string configName)
286 public CtorUseCase1 (string configName, string address)
287 : base (configName, address)
291 public CtorUseCase1 (Binding binding, EndpointAddress address)
292 : base (binding, address)
296 public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
297 : base (i, binding, address)
301 public string Echo (string msg)
303 return Channel.Echo (msg);
307 public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
309 public CtorUseCase2 ()
314 public CtorUseCase2 (string configName)
319 public CtorUseCase2 (string configName, string address)
320 : base (configName, address)
324 public CtorUseCase2 (Binding binding, EndpointAddress address)
325 : base (binding, address)
329 public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
330 : base (i, binding, address)
334 public string Echo (string msg)
336 return Channel.Echo (msg);
340 public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
342 public string Echo (string msg)
344 return Channel.Echo (msg);
350 // For contract that directly receives and sends Message instances.
353 [Ignore ("With Orcas it does not work fine")]
354 public void UseCase2Test ()
356 // almost equivalent to samples/clientbase/samplesvc2.cs
357 ServiceHost host = new ServiceHost (typeof (UseCase2));
358 Binding binding = new BasicHttpBinding ();
359 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
360 host.AddServiceEndpoint (typeof (IUseCase2).FullName,
361 binding, new Uri ("http://localhost:37564"));
365 // almost equivalent to samples/clientbase/samplecli2.cs
366 Binding b = new BasicHttpBinding ();
367 b.SendTimeout = TimeSpan.FromSeconds (15);
368 b.ReceiveTimeout = TimeSpan.FromSeconds (15);
369 UseCase2Proxy proxy = new UseCase2Proxy (
371 new EndpointAddress ("http://localhost:37564/"));
373 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
374 Message res = proxy.Echo (req);
375 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
376 res.WriteMessage (w);
379 if (host.State == CommunicationState.Opened)
381 EnsurePortNonBlocking (37564);
386 public interface IUseCase2
389 Message Echo (Message request);
392 class UseCase2 : IUseCase2
394 public Message Echo (Message request)
396 Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
397 msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
398 //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
403 public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
405 public UseCase2Proxy (Binding binding, EndpointAddress address)
406 : base (binding, address)
410 public Message Echo (Message request)
412 return Channel.Echo (request);
419 [Ignore ("With Orcas it does not work fine")]
420 public void UseCase3 ()
422 // almost equivalent to samples/clientbase/samplesvc3.cs
423 ServiceHost host = new ServiceHost (typeof (MetadataExchange));
424 host.Description.Behaviors.Find<ServiceDebugBehavior> ()
425 .IncludeExceptionDetailInFaults = true;
426 Binding bs = new BasicHttpBinding ();
427 bs.SendTimeout = TimeSpan.FromSeconds (5);
428 bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
429 // magic name that does not require fully qualified name ...
430 host.AddServiceEndpoint ("IMetadataExchange",
431 bs, new Uri ("http://localhost:37564"));
434 // almost equivalent to samples/clientbase/samplecli3.cs
435 Binding bc = new BasicHttpBinding ();
436 bc.SendTimeout = TimeSpan.FromSeconds (5);
437 bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
438 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
440 new EndpointAddress ("http://localhost:37564/"));
443 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
444 Message res = proxy.Get (req);
445 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
446 res.WriteMessage (w);
449 if (host.State == CommunicationState.Opened)
451 EnsurePortNonBlocking (37564);
455 class MetadataExchange : IMetadataExchange
457 public Message Get (Message request)
459 XmlDocument doc = new XmlDocument ();
460 doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
461 return Message.CreateMessage (request.Version,
462 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
463 new XmlNodeReader (doc));
466 public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
468 throw new NotImplementedException ();
471 public Message EndGet (IAsyncResult result)
473 throw new NotImplementedException ();