| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- //
- // ServiceHostTest.cs
- //
- // Author:
- // Ankit Jain <[email protected]>
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2005-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.Generic;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Description;
- using System.ServiceModel.Dispatcher;
- using NUnit.Framework;
- namespace MonoTests.System.ServiceModel
- {
- [TestFixture]
- public class ServiceHostTest
- {
- class MyHost : ServiceHost
- {
- public MyHost (Type type, Uri uri)
- : base (type, uri)
- {
- }
- public IDictionary<string,ContractDescription> ExposedContracts {
- get { return ImplementedContracts; }
- }
- }
- [Test]
- public void Ctor ()
- {
- MyHost host = new MyHost (typeof (Foo), new Uri ("http://localhost"));
- Assert.IsNotNull (host.Description, "#1");
- Assert.AreEqual (typeof (Foo), host.Description.ServiceType, "#1-2");
- Assert.IsNotNull (host.BaseAddresses, "#2");
- Assert.AreEqual (1, host.BaseAddresses.Count, "#3");
- Assert.IsNotNull (host.ChannelDispatchers, "#4");
- Assert.AreEqual (0, host.ChannelDispatchers.Count, "#5");
- Assert.IsNotNull (host.Authorization, "#6");
- Assert.IsNotNull (host.ExposedContracts, "#7");
- // Foo is already in the contracts.
- Assert.AreEqual (1, host.ExposedContracts.Count, "#8");
- // this loop iterates only once.
- foreach (KeyValuePair<string,ContractDescription> e in host.ExposedContracts) {
- // hmm... so, seems like the key is just the full name of the contract type.
- Assert.AreEqual ("MonoTests.System.ServiceModel.ServiceHostTest+Foo", e.Key, "#9");
- ContractDescription cd = e.Value;
- Assert.AreEqual ("Foo", cd.Name, "#10");
- Assert.AreEqual ("http://tempuri.org/", cd.Namespace, "#11");
- }
- }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void CtorNull ()
- {
- new ServiceHost (typeof (Foo), null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CtorServiceTypeNotClass ()
- {
- new ServiceHost (typeof (IBar), new Uri ("http://localhost"));
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CtorRelativeBaseAddress ()
- {
- new ServiceHost (typeof (Foo), new Uri ("test", UriKind.Relative));
- }
-
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CtorMultipleAddressPerScheme ()
- {
- new ServiceHost ( typeof (Foo),
- new Uri ("http://localhost", UriKind.Absolute),
- new Uri ("http://someotherhost", UriKind.Absolute));
- }
- [Test]
- [Ignore ("AddServiceEndpoint part does not work")]
- public void AddServiceEndpoint ()
- {
- ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
- host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
- host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "svc");
- Assert.IsNotNull (host.Description, "#6");
- Assert.IsNotNull (host.Description.Endpoints, "#7");
- Assert.AreEqual (host.Description.Endpoints.Count, 2, "#8");
- Assert.AreEqual ("http://localhost/echo/rel", host.Description.Endpoints [0].Address.Uri.AbsoluteUri, "#9");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpoint1 ()
- {
- ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("ftp://localhost/echo"));
- // ftp does not match BasicHttpBinding
- host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpoint2 ()
- {
- // IBar is not part of the contract
- ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
- host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
- //host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpoint3 ()
- {
- // IBar is not part of the contract
- ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
- host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "rel");
- // host.AddServiceEndpoint ("IBar", new BasicHttpBinding (), "http://localhost/echo/rel");
- }
- [Test]
- public void AddServiceEndpoint4 ()
- {
- ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
- host.AddServiceEndpoint ("MonoTests.System.ServiceModel.ServiceHostTest+IBaz", new BasicHttpBinding (), "rel");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpoint5 ()
- {
- ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
- // Full type name is expected here (see AddServiceEndpoint4).
- host.AddServiceEndpoint ("IBaz", new BasicHttpBinding (), "rel");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpoint6 ()
- {
- ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
- host.AddServiceEndpoint ("ISuchTypeDoesNotExist", new BasicHttpBinding (), "rel");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpointMex ()
- {
- using (ServiceHost h = new ServiceHost (typeof (Foo), new Uri ("http://localhost:8080"))) {
- // it expects ServiceMetadataBehavior
- h.AddServiceEndpoint (ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
- }
- }
- [Test]
- public void AddServiceEndpointMetadataExchange ()
- {
- ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
- // strange, but unlike above, it is accepted. The only difference I can see is the binding name.
- host.AddServiceEndpoint ("IMetadataExchange",
- new BasicHttpBinding (),
- "http://localhost:8080");
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void AddServiceEndpointMetadataExchangeFullNameFails ()
- {
- ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
- host.AddServiceEndpoint ("System.ServiceModel.Description.IMetadataExchange",
- new BasicHttpBinding (),
- "http://localhost:8080");
- }
- [Test]
- public void InstanceWithNonSingletonMode ()
- {
- ServiceHost host = new ServiceHost (
- new NonSingletonService ());
- Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "premise1");
- host.AddServiceEndpoint (
- typeof (NonSingletonService),
- new BasicHttpBinding (),
- new Uri ("http://localhost:37564/s1"));
- // in case Open() didn't fail, we need to close the host.
- // And even if Close() caused the expected exception,
- // the test should still fail.
- try {
- host.Open ();
- try {
- if (host.State == CommunicationState.Opened)
- host.Close ();
- } catch (InvalidOperationException) {
- }
- Assert.Fail ("InstanceContextMode was not checked");
- } catch (InvalidOperationException) {
- }
- }
- [Test]
- public void InstanceWithSingletonMode ()
- {
- SingletonService instance = new SingletonService ();
- ServiceHost host = new ServiceHost (instance);
- Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "#1");
- host.AddServiceEndpoint (
- typeof (SingletonService),
- new BasicHttpBinding (),
- new Uri ("http://localhost:37564/s2"));
- // in case Open() didn't fail, we need to close the host.
- // And even if Close() caused the expected exception,
- // the test should still fail.
- try {
- host.Open ();
- ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [0];
- DispatchRuntime dr = cd.Endpoints [0].DispatchRuntime;
- Assert.IsNotNull (dr.InstanceContextProvider, "#2");
- InstanceContext ctx = dr.InstanceContextProvider.GetExistingInstanceContext (null, null);
- Assert.IsNotNull (ctx, "#3");
- Assert.AreEqual (instance, ctx.GetServiceInstance (), "#4");
- } finally {
- if (host.State == CommunicationState.Opened)
- host.Close ();
- }
- }
- [ServiceContract]
- interface IBar
- {
- }
- [ServiceContract]
- class Foo
- {
- [OperationContract]
- public void SayWhat () { }
- }
- [ServiceContract]
- interface IBaz
- {
- [OperationContract]
- string Echo (string source);
- }
- class Baz : IBaz
- {
- public string Echo (string source)
- {
- return source;
- }
- }
- class MyMetadataExchange : IMetadataExchange
- {
- public Message Get (Message req)
- {
- throw new NotImplementedException ();
- }
- public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
- {
- throw new NotImplementedException ();
- }
- public Message EndGet (IAsyncResult result)
- {
- throw new NotImplementedException ();
- }
- }
- [ServiceContract]
- public class NonSingletonService
- {
- [OperationContract]
- public void Process (string input)
- {
- }
- }
- [ServiceContract]
- [ServiceBehavior (InstanceContextMode = InstanceContextMode.Single)]
- public class SingletonService
- {
- [OperationContract]
- public void Process (string input)
- {
- }
- }
- }
- }
|