Browse Source

2009-10-15 Atsushi Enomoto <[email protected]>

	* ServiceHostBase.cs : on opening the host, check service endpoints
	  to make sure if there is at least one "non-mex" endpoint.
	  Fix couple of typos.

	* ServiceHostBaseTest.cs : add test for checking non-mex contract
	  existence.


svn path=/trunk/mcs/; revision=144176
Atsushi Eno 16 years ago
parent
commit
6972c12e9f

+ 6 - 0
mcs/class/System.ServiceModel/System.ServiceModel/ChangeLog

@@ -1,3 +1,9 @@
+2009-10-15  Atsushi Enomoto  <[email protected]>
+
+	* ServiceHostBase.cs : on opening the host, check service endpoints
+	  to make sure if there is at least one "non-mex" endpoint.
+	  Fix couple of typos.
+
 2009-10-09  Atsushi Enomoto  <[email protected]>
 
 	* InstanceContext.cs : new constraints on CommunicationObject

+ 20 - 14
mcs/class/System.ServiceModel/System.ServiceModel/ServiceHostBase.cs

@@ -28,6 +28,7 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.Linq;
 using System.ServiceModel.Channels;
 using System.ServiceModel.Configuration;
 using System.ServiceModel.Description;
@@ -84,16 +85,17 @@ namespace System.ServiceModel
 			}
 		}
 
-		internal Uri CreateUri (string sheme, Uri relatieUri) {
-			Uri baseUri = base_addresses.Contains (sheme) ? base_addresses [sheme] : null;
+		internal Uri CreateUri (string scheme, Uri relativeUri) {
+			Uri baseUri = base_addresses.Contains (scheme) ? base_addresses [scheme] : null;
 
-			if (relatieUri == null)
+			if (relativeUri == null)
 				return baseUri;
-			if (relatieUri.IsAbsoluteUri)
-				return relatieUri;
+			if (relativeUri.IsAbsoluteUri)
+				return relativeUri;
 			if (baseUri == null)
 				return null;
-			return new Uri (baseUri, relatieUri);
+			var s = relativeUri.ToString ();
+			return new Uri (baseUri, s.Length > 0 && s [0] == '/' ? '.' + s : s);
 		}
 
 		public ChannelDispatcherCollection ChannelDispatchers {
@@ -217,13 +219,14 @@ namespace System.ServiceModel
 				return help_page_contract;
 			case "IMetadataExchange":
 				// this is certainly looking special (or we may 
-				// be missing something around ServiceMetadataExtension)
-				// FIXME: this check breaks initialization by configuration
-				// (as ApplyConfiguration() processes all <endpoint> elements
-				// before ServiceMetadataBehavior.ApplyDispatchBehavior()).
-				// So, disable it so far. (it is mostly harmless).
-				//if (Extensions.Find<ServiceMetadataExtension> () == null)
-				//	break;
+				// be missing something around ServiceMetadataExtension).
+				// It seems .NET WCF has some "infrastructure"
+				// endpoints. .NET ServiceHost fails to Open()
+				// if it was added only IMetadataExchange 
+				// endpoint (and you'll see the word
+				// "infrastructure" in the exception message).
+				if (Description.Behaviors.Find<ServiceMetadataBehavior> () == null)
+					break;
 				if (mex_contract == null)
 					mex_contract = ContractDescription.GetContract (typeof (IMetadataExchange));
 				return mex_contract;
@@ -406,7 +409,10 @@ namespace System.ServiceModel
 				b.Validate (Description, this);
 			foreach (ServiceEndpoint endPoint in Description.Endpoints)
 				endPoint.Validate ();
-		}		
+
+			if (Description.Endpoints.FirstOrDefault (e => e.Contract != mex_contract) == null)
+				throw new InvalidOperationException ("The ServiceHost must have at least one application endpoint (that does not include metadata exchange contract) defined by either configuration, behaviors or call to AddServiceEndpoint methods.");
+		}
 
 		private void ApplyDispatchBehavior (EndpointDispatcher ed, ServiceEndpoint endPoint)
 		{

+ 5 - 0
mcs/class/System.ServiceModel/Test/System.ServiceModel/ChangeLog

@@ -1,3 +1,8 @@
+2009-10-15  Atsushi Enomoto  <[email protected]>
+
+	* ServiceHostBaseTest.cs : add test for checking non-mex contract
+	  existence.
+
 2009-10-07  Sebastien Pouliot  <[email protected]>
 
 	* OperationContextTest.cs: Add test case for OperationContext.Current

+ 18 - 0
mcs/class/System.ServiceModel/Test/System.ServiceModel/ServiceHostBaseTest.cs

@@ -302,6 +302,24 @@ namespace MonoTests.System.ServiceModel
 			Assert.AreEqual ("http://localhost:37564/", se.ListenUri.AbsoluteUri, "#2");
 		}
 
+		[Test]
+		[ExpectedException (typeof (InvalidOperationException))]
+		public void AddServiceEndpointOnlyMex ()
+		{
+			var host = new ServiceHost (typeof (AllActions),
+				new Uri ("http://localhost:37564"));
+			host.Description.Behaviors.Add (new ServiceMetadataBehavior ());
+			host.AddServiceEndpoint ("IMetadataExchange",
+				new BasicHttpBinding (), "/wsdl");
+			host.Open ();
+			try {
+				// to make sure that throwing IOE from here does not count.
+				host.Close ();
+			} catch {
+			}
+			Assert.Fail ("should not open");
+		}
+
 		#region helpers
 
 		public enum Stage