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 using MonoTests.Helpers;
41 namespace MonoTests.System.ServiceModel
44 public class ClientBaseTest
48 [ExpectedException (typeof (InvalidOperationException))]
49 public void GenericTypeArgumentIsServiceContract ()
51 new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
56 public class MyClientBase<T> : ClientBase<T>
58 public MyClientBase (Binding binding, EndpointAddress address)
59 : base (binding, address)
64 public class MyClientBase1 : ClientBase<TestService>
66 public MyClientBase1 (Binding binding, EndpointAddress address)
67 : base (binding, address)
73 public void ClassTypeArg ()
75 new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
80 public interface ITestService
83 string Foo (string arg);
86 public class TestService : ITestService
88 public string Foo (string arg)
95 public interface ITestService2
98 void Bar (string arg);
101 public class TestService2 : ITestService2
103 public void Bar (string arg)
109 [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
112 Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
113 Assert.IsTrue (t.IsGenericParameter);
114 Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
117 class MyChannelFactory<T> : ChannelFactory<T>
119 public MyChannelFactory (Binding b, EndpointAddress e)
124 public IChannelFactory GimmeFactory ()
126 return CreateFactory ();
135 [ExpectedException (typeof (ArgumentNullException))]
136 public void ClientBaseCtorArgsTest1 ()
138 new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
142 [ExpectedException (typeof (ArgumentNullException))]
143 public void ClientBaseCtorArgsTest2 ()
145 new CtorUseCase1 (null, new EndpointAddress ("http://test"));
149 [ExpectedException (typeof (ArgumentNullException))]
150 public void ClientBaseCtorArgsTest3 ()
152 new CtorUseCase1 (null, "http://test");
156 [ExpectedException (typeof (ArgumentNullException))]
157 public void ClientBaseCtorArgsTest4 ()
159 new CtorUseCase1 ("CtorUseCase1_1", null);
163 [ExpectedException (typeof (ArgumentNullException))]
164 public void ClientBaseCtorArgsTest5 ()
166 new CtorUseCase1 (new BasicHttpBinding (), null);
170 [ExpectedException (typeof (InvalidOperationException))]
171 public void ClientBaseCtorArgsTest6 ()
173 new CtorUseCase1 ("CtorUseCase1_Incorrect");
177 [ExpectedException (typeof (InvalidOperationException))]
178 public void ClientBaseCtorArgsTest7 ()
184 [ExpectedException (typeof (InvalidOperationException))]
185 public void ClientBaseCtorConfigAmbiguityTest ()
191 [ExpectedException (typeof (InvalidOperationException))]
192 public void ClientBaseCtorConfigAmbiguityTest2 ()
194 new CtorUseCase2 ("*");
198 [Ignore ("fails under .NET; I never bothered to fix the test")]
199 public void ClientBaseConfigEmptyCtor ()
205 [Ignore ("fails under .NET; I never bothered to fix the test")]
206 public void ClientBaseConfigCtor ()
208 new CtorUseCase1 ("CtorUseCase1_1");
212 [Ignore ("fails under .NET; I never bothered to fix the test")]
213 public void ClientBaseConfigCtorUsingDefault ()
215 new CtorUseCase1 ("*");
219 [Ignore ("With Orcas it does not work fine")]
220 public void UseCase1Test ()
222 int port = NetworkHelpers.FindFreePort ();
223 // almost equivalent to samples/clientbase/samplesvc.cs
224 using (host = new ServiceHost (typeof (UseCase1))) {
225 Binding binding = new BasicHttpBinding ();
226 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
227 host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:" + port));
230 // almost equivalent to samples/clientbase/samplecli.cs
231 using (UseCase1Proxy proxy = new UseCase1Proxy (
232 new BasicHttpBinding (),
233 new EndpointAddress ("http://localhost:" + port))) {
235 Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
238 EnsurePortNonBlocking (port);
241 void EnsurePortNonBlocking (int port)
243 TcpListener l = new TcpListener (port);
244 l.ExclusiveAddressUse = true;
250 public interface IUseCase1
253 string Echo (string msg);
256 public class UseCase1 : IUseCase1
258 public string Echo (string msg)
264 public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
266 public UseCase1Proxy (Binding binding, EndpointAddress address)
267 : base (binding, address)
271 public string Echo (string msg)
273 return Channel.Echo (msg);
277 public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
279 public CtorUseCase1 ()
284 public CtorUseCase1 (string configName)
289 public CtorUseCase1 (string configName, string address)
290 : base (configName, address)
294 public CtorUseCase1 (Binding binding, EndpointAddress address)
295 : base (binding, address)
299 public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
300 : base (i, binding, address)
304 public string Echo (string msg)
306 return Channel.Echo (msg);
310 public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
312 public CtorUseCase2 ()
317 public CtorUseCase2 (string configName)
322 public CtorUseCase2 (string configName, string address)
323 : base (configName, address)
327 public CtorUseCase2 (Binding binding, EndpointAddress address)
328 : base (binding, address)
332 public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
333 : base (i, binding, address)
337 public string Echo (string msg)
339 return Channel.Echo (msg);
343 public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
345 public string Echo (string msg)
347 return Channel.Echo (msg);
353 // For contract that directly receives and sends Message instances.
356 [Ignore ("With Orcas it does not work fine")]
357 public void UseCase2Test ()
359 // almost equivalent to samples/clientbase/samplesvc2.cs
360 ServiceHost host = new ServiceHost (typeof (UseCase2));
361 Binding binding = new BasicHttpBinding ();
362 binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
363 int port = NetworkHelpers.FindFreePort ();
364 host.AddServiceEndpoint (typeof (IUseCase2).FullName,
365 binding, new Uri ("http://localhost:" + port));
369 // almost equivalent to samples/clientbase/samplecli2.cs
370 Binding b = new BasicHttpBinding ();
371 b.SendTimeout = TimeSpan.FromSeconds (15);
372 b.ReceiveTimeout = TimeSpan.FromSeconds (15);
373 UseCase2Proxy proxy = new UseCase2Proxy (
375 new EndpointAddress ("http://localhost:" + port + "/"));
377 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
378 Message res = proxy.Echo (req);
379 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
380 res.WriteMessage (w);
383 if (host.State == CommunicationState.Opened)
385 EnsurePortNonBlocking (port);
390 public interface IUseCase2
393 Message Echo (Message request);
396 class UseCase2 : IUseCase2
398 public Message Echo (Message request)
400 Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
401 msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
402 //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
407 public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
409 public UseCase2Proxy (Binding binding, EndpointAddress address)
410 : base (binding, address)
414 public Message Echo (Message request)
416 return Channel.Echo (request);
423 [Ignore ("With Orcas it does not work fine")]
424 public void UseCase3 ()
426 // almost equivalent to samples/clientbase/samplesvc3.cs
427 ServiceHost host = new ServiceHost (typeof (MetadataExchange));
428 host.Description.Behaviors.Find<ServiceDebugBehavior> ()
429 .IncludeExceptionDetailInFaults = true;
430 Binding bs = new BasicHttpBinding ();
431 bs.SendTimeout = TimeSpan.FromSeconds (5);
432 bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
433 int port = NetworkHelpers.FindFreePort ();
434 // magic name that does not require fully qualified name ...
435 host.AddServiceEndpoint ("IMetadataExchange",
436 bs, new Uri ("http://localhost:" + port));
439 // almost equivalent to samples/clientbase/samplecli3.cs
440 Binding bc = new BasicHttpBinding ();
441 bc.SendTimeout = TimeSpan.FromSeconds (5);
442 bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
443 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
445 new EndpointAddress ("http://localhost:" + port + "/"));
448 Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
449 Message res = proxy.Get (req);
450 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
451 res.WriteMessage (w);
454 if (host.State == CommunicationState.Opened)
456 EnsurePortNonBlocking (port);
460 class MetadataExchange : IMetadataExchange
462 public Message Get (Message request)
464 XmlDocument doc = new XmlDocument ();
465 doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
466 return Message.CreateMessage (request.Version,
467 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
468 new XmlNodeReader (doc));
471 public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
473 throw new NotImplementedException ();
476 public Message EndGet (IAsyncResult result)
478 throw new NotImplementedException ();
483 public void ConstructorServiceEndpoint ()
485 // It is okay to pass ServiceEndpoint that does not have Binding or EndpointAddress.
486 new MyClient (new ServiceEndpoint (ContractDescription.GetContract (typeof (IMetadataExchange)), null, null));
488 new MyClient ((Binding) null, (EndpointAddress) null);
489 Assert.Fail ("ArgumentNullException is expected");
490 } catch (ArgumentNullException) {
494 class MyClient : ClientBase<IMetadataExchange>
496 public MyClient (ServiceEndpoint endpoint)
501 public MyClient (Binding binding, EndpointAddress address)
502 : base (binding, address)