5 // Ankit Jain <jankit@novell.com>
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Dispatcher;
35 using NUnit.Framework;
37 namespace MonoTests.System.ServiceModel
40 public class ServiceHostTest
42 class MyHost : ServiceHost
44 public MyHost (Type type, Uri uri)
49 public IDictionary<string,ContractDescription> ExposedContracts {
50 get { return ImplementedContracts; }
57 MyHost host = new MyHost (typeof (Foo), new Uri ("http://localhost"));
58 Assert.IsNotNull (host.Description, "#1");
59 Assert.AreEqual (typeof (Foo), host.Description.ServiceType, "#1-2");
60 Assert.IsNotNull (host.BaseAddresses, "#2");
61 Assert.AreEqual (1, host.BaseAddresses.Count, "#3");
63 Assert.IsNotNull (host.ChannelDispatchers, "#4");
64 Assert.AreEqual (0, host.ChannelDispatchers.Count, "#5");
65 Assert.IsNotNull (host.Authorization, "#6");
66 Assert.IsNotNull (host.ExposedContracts, "#7");
67 // Foo is already in the contracts.
68 Assert.AreEqual (1, host.ExposedContracts.Count, "#8");
69 // this loop iterates only once.
70 foreach (KeyValuePair<string,ContractDescription> e in host.ExposedContracts) {
71 // hmm... so, seems like the key is just the full name of the contract type.
72 Assert.AreEqual ("MonoTests.System.ServiceModel.ServiceHostTest+Foo", e.Key, "#9");
73 ContractDescription cd = e.Value;
74 Assert.AreEqual ("Foo", cd.Name, "#10");
75 Assert.AreEqual ("http://tempuri.org/", cd.Namespace, "#11");
80 [ExpectedException (typeof (ArgumentNullException))]
81 public void CtorNull ()
83 new ServiceHost (typeof (Foo), null);
87 [ExpectedException (typeof (ArgumentException))]
88 public void CtorServiceTypeNotClass ()
90 new ServiceHost (typeof (IBar), new Uri ("http://localhost"));
94 [ExpectedException (typeof (ArgumentException))]
95 public void CtorRelativeBaseAddress ()
97 new ServiceHost (typeof (Foo), new Uri ("test", UriKind.Relative));
101 [ExpectedException (typeof (ArgumentException))]
102 public void CtorMultipleAddressPerScheme ()
104 new ServiceHost ( typeof (Foo),
105 new Uri ("http://localhost", UriKind.Absolute),
106 new Uri ("http://someotherhost", UriKind.Absolute));
110 public void AddServiceEndpoint ()
112 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
113 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
114 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "svc");
116 Assert.IsNotNull (host.Description, "#6");
117 Assert.IsNotNull (host.Description.Endpoints, "#7");
118 Assert.AreEqual (host.Description.Endpoints.Count, 2, "#8");
119 Assert.AreEqual ("http://localhost/echo/rel", host.Description.Endpoints [0].Address.Uri.AbsoluteUri, "#9");
123 [ExpectedException (typeof (InvalidOperationException))]
124 public void AddServiceEndpoint1 ()
126 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("ftp://localhost/echo"));
127 // ftp does not match BasicHttpBinding
128 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
132 [ExpectedException (typeof (InvalidOperationException))]
133 public void AddServiceEndpoint2 ()
135 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
136 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
137 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel"); // duplicate URI
140 host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
144 [ExpectedException (typeof (InvalidOperationException))]
145 public void AddServiceEndpoint2_2 ()
147 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
148 // same as above, but through Endpoints.Add()
149 host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (Foo)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo/rel")));
150 host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (Foo)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo/rel")));
153 host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
157 [ExpectedException (typeof (InvalidOperationException))]
158 public void AddServiceEndpoint2_3 ()
160 ServiceHost host = new ServiceHost (typeof (HogeFuga), new Uri ("http://localhost/echo"));
161 host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (IHoge)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo")));
162 host.Description.Endpoints.Add (new ServiceEndpoint (ContractDescription.GetContract (typeof (IFuga)), new BasicHttpBinding (), new EndpointAddress ("http://localhost/echo")));
164 // Different contracts unlike previous two cases.
165 // If two or more endpoints are bound to the same listen
166 // URI, then they must share the same instance.
169 host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
173 public void AddServiceEndpoint2_4 ()
175 ServiceHost host = new ServiceHost (typeof (HogeFuga), new Uri ("http://localhost:37564"));
176 var binding = new BasicHttpBinding ();
177 host.AddServiceEndpoint (typeof (IHoge), binding, new Uri ("http://localhost:37564"));
178 host.AddServiceEndpoint (typeof (IFuga), binding, new Uri ("http://localhost:37564"));
180 // Use the same binding, results in one ChannelDispatcher (actually two, for metadata/debug behavior).
183 Assert.AreEqual (2, host.ChannelDispatchers.Count, "#1");
184 foreach (ChannelDispatcher cd in host.ChannelDispatchers) {
185 if (cd.BindingName != binding.Name)
187 Assert.AreEqual (2, cd.Endpoints.Count, "#2");
195 [ExpectedException (typeof (InvalidOperationException))]
196 public void AddServiceEndpoint3 ()
198 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
199 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "rel");
200 host.AddServiceEndpoint (typeof (Foo), new BasicHttpBinding (), "http://localhost/echo/rel"); // duplicate URI when resolved
203 host.Close (); // should not reach here. It is to make sure to close unexpectedly opened host.
207 public void AddServiceEndpoint4 ()
209 ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
210 host.AddServiceEndpoint ("MonoTests.System.ServiceModel.ServiceHostTest+IBaz", new BasicHttpBinding (), "rel");
214 [ExpectedException (typeof (InvalidOperationException))]
215 public void AddServiceEndpoint5 ()
217 ServiceHost host = new ServiceHost (typeof (Baz), new Uri ("http://localhost/echo"));
219 // Full type name is expected here (see AddServiceEndpoint4).
220 host.AddServiceEndpoint ("IBaz", new BasicHttpBinding (), "rel");
224 [ExpectedException (typeof (InvalidOperationException))]
225 public void AddServiceEndpoint6 ()
227 ServiceHost host = new ServiceHost (typeof (Foo), new Uri ("http://localhost/echo"));
228 host.AddServiceEndpoint ("ISuchTypeDoesNotExist", new BasicHttpBinding (), "rel");
232 [ExpectedException (typeof (InvalidOperationException))]
233 public void AddServiceEndpointMexWithNoImpl ()
235 using (ServiceHost h = new ServiceHost (typeof (Foo), new Uri ("http://localhost:8080"))) {
236 // it expects ServiceMetadataBehavior
237 h.AddServiceEndpoint (ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding (), "mex");
242 public void AddServiceEndpointMetadataExchange ()
244 // MyMetadataExchange implements IMetadataExchange
245 ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
246 host.AddServiceEndpoint ("IMetadataExchange",
247 new BasicHttpBinding (),
248 "http://localhost:8080/");
252 [ExpectedException (typeof (InvalidOperationException))]
253 public void AddServiceEndpointMetadataExchangeFullNameFails ()
255 ServiceHost host = new ServiceHost (typeof (MyMetadataExchange));
256 host.AddServiceEndpoint ("System.ServiceModel.Description.IMetadataExchange",
257 new BasicHttpBinding (),
258 "http://localhost:8080");
262 public void InstanceWithNonSingletonMode ()
264 ServiceHost host = new ServiceHost (
265 new NonSingletonService ());
266 Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "premise1");
267 host.AddServiceEndpoint (
268 typeof (NonSingletonService),
269 new BasicHttpBinding (),
270 new Uri ("http://localhost:37564/s1"));
272 // in case Open() didn't fail, we need to close the host.
273 // And even if Close() caused the expected exception,
274 // the test should still fail.
278 if (host.State == CommunicationState.Opened)
280 } catch (InvalidOperationException) {
282 Assert.Fail ("InstanceContextMode was not checked");
283 } catch (InvalidOperationException) {
289 public void InstanceWithSingletonMode ()
291 SingletonService instance = new SingletonService ();
292 ServiceHost host = new ServiceHost (instance);
293 Assert.IsNotNull (host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().GetWellKnownSingleton (), "#1");
294 host.AddServiceEndpoint (
295 typeof (SingletonService),
296 new BasicHttpBinding (),
297 new Uri ("http://localhost:37564/s2"));
299 // in case Open() didn't fail, we need to close the host.
300 // And even if Close() caused the expected exception,
301 // the test should still fail.
304 ChannelDispatcher cd = (ChannelDispatcher) host.ChannelDispatchers [0];
305 DispatchRuntime dr = cd.Endpoints [0].DispatchRuntime;
306 Assert.IsNotNull (dr.InstanceContextProvider, "#2");
307 InstanceContext ctx = dr.InstanceContextProvider.GetExistingInstanceContext (null, null);
308 Assert.IsNotNull (ctx, "#3");
309 Assert.AreEqual (instance, ctx.GetServiceInstance (), "#4");
311 if (host.State == CommunicationState.Opened)
325 public void SayWhat () { }
332 string Echo (string source);
349 class HogeFuga : IHoge, IFuga
351 public void DoX () {}
352 public void DoY () {}
357 public string Echo (string source)
363 class MyMetadataExchange : IMetadataExchange
365 public Message Get (Message req)
367 throw new NotImplementedException ();
370 public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
372 throw new NotImplementedException ();
375 public Message EndGet (IAsyncResult result)
377 throw new NotImplementedException ();
382 public class NonSingletonService
385 public void Process (string input)
391 [ServiceBehavior (InstanceContextMode = InstanceContextMode.Single)]
392 public class SingletonService
395 public void Process (string input)