| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- //
- // ClientBaseTest.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Net.Sockets;
- using System.Reflection;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Description;
- using System.Xml;
- using NUnit.Framework;
- namespace MonoTests.System.ServiceModel
- {
- [TestFixture]
- public class ClientBaseTest
- {
- /*
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void GenericTypeArgumentIsServiceContract ()
- {
- new MyClientBase<ICloneable> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:4126"));
- }
- */
- /*
- public class MyClientBase<T> : ClientBase<T>
- {
- public MyClientBase (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- }
- public class MyClientBase1 : ClientBase<TestService>
- {
- public MyClientBase1 (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- }
- [Test]
- public void ClassTypeArg ()
- {
- new MyClientBase1 (new BasicHttpBinding (), new EndpointAddress ("urn:dummy"));
- }
- */
- [ServiceContract]
- public interface ITestService
- {
- [OperationContract]
- string Foo (string arg);
- }
- public class TestService : ITestService
- {
- public string Foo (string arg)
- {
- return arg;
- }
- }
- [ServiceContract]
- public interface ITestService2
- {
- [OperationContract]
- void Bar (string arg);
- }
- public class TestService2 : ITestService2
- {
- public void Bar (string arg)
- {
- }
- }
- [Test]
- [Ignore ("hmm, this test shows that MSDN documentation does not match the fact.")]
- public void Foo ()
- {
- Type t = typeof (ClientBase<ITestService>).GetGenericTypeDefinition ().GetGenericArguments () [0];
- Assert.IsTrue (t.IsGenericParameter);
- Assert.AreEqual (GenericParameterAttributes.None, t.GenericParameterAttributes);
- }
- class MyChannelFactory<T> : ChannelFactory<T>
- {
- public MyChannelFactory (Binding b, EndpointAddress e)
- : base (b, e)
- {
- }
- public IChannelFactory GimmeFactory ()
- {
- return CreateFactory ();
- }
- }
- #region UseCase1
- ServiceHost host;
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ClientBaseCtorArgsTest1 ()
- {
- new CtorUseCase1 (null, new BasicHttpBinding (), new EndpointAddress ("http://test"));
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ClientBaseCtorArgsTest2 ()
- {
- new CtorUseCase1 (null, new EndpointAddress ("http://test"));
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ClientBaseCtorArgsTest3 ()
- {
- new CtorUseCase1 (null, "http://test");
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ClientBaseCtorArgsTest4 ()
- {
- new CtorUseCase1 ("CtorUseCase1_1", null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void ClientBaseCtorArgsTest5 ()
- {
- new CtorUseCase1 (new BasicHttpBinding (), null);
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ClientBaseCtorArgsTest6 ()
- {
- new CtorUseCase1 ("CtorUseCase1_Incorrect");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ClientBaseCtorArgsTest7 ()
- {
- new CtorUseCase3 ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ClientBaseCtorConfigAmbiguityTest ()
- {
- new CtorUseCase2 ();
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void ClientBaseCtorConfigAmbiguityTest2 ()
- {
- new CtorUseCase2 ("*");
- }
- [Test]
- [Ignore ("fails under .NET; I never bothered to fix the test")]
- public void ClientBaseConfigEmptyCtor ()
- {
- new CtorUseCase1 ();
- }
- [Test]
- [Ignore ("fails under .NET; I never bothered to fix the test")]
- public void ClientBaseConfigCtor ()
- {
- new CtorUseCase1 ("CtorUseCase1_1");
- }
- [Test]
- [Ignore ("fails under .NET; I never bothered to fix the test")]
- public void ClientBaseConfigCtorUsingDefault ()
- {
- new CtorUseCase1 ("*");
- }
- [Test]
- [Ignore ("With Orcas it does not work fine")]
- public void UseCase1Test ()
- {
- // almost equivalent to samples/clientbase/samplesvc.cs
- using (host = new ServiceHost (typeof (UseCase1))) {
- Binding binding = new BasicHttpBinding ();
- binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
- host.AddServiceEndpoint (typeof (IUseCase1).FullName, binding, new Uri ("http://localhost:37564"));
- host.Open ();
- // almost equivalent to samples/clientbase/samplecli.cs
- using (UseCase1Proxy proxy = new UseCase1Proxy (
- new BasicHttpBinding (),
- new EndpointAddress ("http://localhost:37564"))) {
- proxy.Open ();
- Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"));
- }
- }
- EnsurePortNonBlocking (37564);
- }
- void EnsurePortNonBlocking (int port)
- {
- TcpListener l = new TcpListener (port);
- l.ExclusiveAddressUse = true;
- l.Start ();
- l.Stop ();
- }
- [ServiceContract]
- public interface IUseCase1
- {
- [OperationContract]
- string Echo (string msg);
- }
- public class UseCase1 : IUseCase1
- {
- public string Echo (string msg)
- {
- return msg + msg;
- }
- }
- public class UseCase1Proxy : ClientBase<IUseCase1>, IUseCase1
- {
- public UseCase1Proxy (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- public string Echo (string msg)
- {
- return Channel.Echo (msg);
- }
- }
- public class CtorUseCase1 : ClientBase<ICtorUseCase1>, ICtorUseCase1
- {
- public CtorUseCase1 ()
- : base ()
- {
- }
- public CtorUseCase1 (string configName)
- : base (configName)
- {
- }
- public CtorUseCase1 (string configName, string address)
- : base (configName, address)
- {
- }
- public CtorUseCase1 (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- public CtorUseCase1 (InstanceContext i, Binding binding, EndpointAddress address)
- : base (i, binding, address)
- {
- }
- public string Echo (string msg)
- {
- return Channel.Echo (msg);
- }
- }
- public class CtorUseCase2 : ClientBase<ICtorUseCase2>, ICtorUseCase2
- {
- public CtorUseCase2 ()
- : base ()
- {
- }
- public CtorUseCase2 (string configName)
- : base (configName)
- {
- }
- public CtorUseCase2 (string configName, string address)
- : base (configName, address)
- {
- }
- public CtorUseCase2 (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- public CtorUseCase2 (InstanceContext i, Binding binding, EndpointAddress address)
- : base (i, binding, address)
- {
- }
- public string Echo (string msg)
- {
- return Channel.Echo (msg);
- }
- }
- public class CtorUseCase3 : ClientBase<ICtorUseCase3>, ICtorUseCase3
- {
- public string Echo (string msg)
- {
- return Channel.Echo (msg);
- }
- }
- #endregion
- // For contract that directly receives and sends Message instances.
- #region UseCase2
- [Test]
- [Ignore ("With Orcas it does not work fine")]
- public void UseCase2Test ()
- {
- // almost equivalent to samples/clientbase/samplesvc2.cs
- ServiceHost host = new ServiceHost (typeof (UseCase2));
- Binding binding = new BasicHttpBinding ();
- binding.ReceiveTimeout = TimeSpan.FromSeconds (15);
- host.AddServiceEndpoint (typeof (IUseCase2).FullName,
- binding, new Uri ("http://localhost:37564"));
- try {
- host.Open ();
- // almost equivalent to samples/clientbase/samplecli2.cs
- Binding b = new BasicHttpBinding ();
- b.SendTimeout = TimeSpan.FromSeconds (15);
- b.ReceiveTimeout = TimeSpan.FromSeconds (15);
- UseCase2Proxy proxy = new UseCase2Proxy (
- b,
- new EndpointAddress ("http://localhost:37564/"));
- proxy.Open ();
- Message req = Message.CreateMessage (MessageVersion.Soap11, "http://tempuri.org/IUseCase2/Echo");
- Message res = proxy.Echo (req);
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
- res.WriteMessage (w);
- }
- } finally {
- if (host.State == CommunicationState.Opened)
- host.Close ();
- EnsurePortNonBlocking (37564);
- }
- }
- [ServiceContract]
- public interface IUseCase2
- {
- [OperationContract]
- Message Echo (Message request);
- }
- class UseCase2 : IUseCase2
- {
- public Message Echo (Message request)
- {
- Message msg = Message.CreateMessage (request.Version, request.Headers.Action + "Response");
- msg.Headers.Add (MessageHeader.CreateHeader ("hoge", "urn:hoge", "heh"));
- //msg.Headers.Add (MessageHeader.CreateHeader ("test", "http://schemas.microsoft.com/ws/2005/05/addressing/none", "testing"));
- return msg;
- }
- }
- public class UseCase2Proxy : ClientBase<IUseCase2>, IUseCase2
- {
- public UseCase2Proxy (Binding binding, EndpointAddress address)
- : base (binding, address)
- {
- }
- public Message Echo (Message request)
- {
- return Channel.Echo (request);
- }
- }
- #endregion
- [Test]
- [Ignore ("With Orcas it does not work fine")]
- public void UseCase3 ()
- {
- // almost equivalent to samples/clientbase/samplesvc3.cs
- ServiceHost host = new ServiceHost (typeof (MetadataExchange));
- host.Description.Behaviors.Find<ServiceDebugBehavior> ()
- .IncludeExceptionDetailInFaults = true;
- Binding bs = new BasicHttpBinding ();
- bs.SendTimeout = TimeSpan.FromSeconds (5);
- bs.ReceiveTimeout = TimeSpan.FromSeconds (5);
- // magic name that does not require fully qualified name ...
- host.AddServiceEndpoint ("IMetadataExchange",
- bs, new Uri ("http://localhost:37564"));
- try {
- host.Open ();
- // almost equivalent to samples/clientbase/samplecli3.cs
- Binding bc = new BasicHttpBinding ();
- bc.SendTimeout = TimeSpan.FromSeconds (5);
- bc.ReceiveTimeout = TimeSpan.FromSeconds (5);
- MetadataExchangeProxy proxy = new MetadataExchangeProxy (
- bc,
- new EndpointAddress ("http://localhost:37564/"));
- proxy.Open ();
- Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
- Message res = proxy.Get (req);
- using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
- res.WriteMessage (w);
- }
- } finally {
- if (host.State == CommunicationState.Opened)
- host.Close ();
- EnsurePortNonBlocking (37564);
- }
- }
- class MetadataExchange : IMetadataExchange
- {
- public Message Get (Message request)
- {
- XmlDocument doc = new XmlDocument ();
- doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
- return Message.CreateMessage (request.Version,
- "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
- new XmlNodeReader (doc));
- }
- public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
- {
- throw new NotImplementedException ();
- }
- public Message EndGet (IAsyncResult result)
- {
- throw new NotImplementedException ();
- }
- }
- }
- }
|