New tests.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / ServiceHostBase.cs
index df9e8cf49a8cc0a7ea54ef5b8b8400ae317a8d0c..81b5608aabdaa9e6acbd19776e59d7d5e5c23a3a 100644 (file)
@@ -41,6 +41,9 @@ namespace System.ServiceModel
        public abstract partial class ServiceHostBase
                : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
        {
+               // It is used for mapping a ServiceHostBase to HttpChannelListener precisely.
+               internal static ServiceHostBase CurrentServiceHostHack;
+
                ServiceCredentials credentials;
                ServiceDescription description;
                UriSchemeKeyedCollection base_addresses;
@@ -377,7 +380,7 @@ namespace System.ServiceModel
                        //can change the collection in the description during the behaviors events.
                        ServiceEndpoint[] endPoints = new ServiceEndpoint[Description.Endpoints.Count];
                        Description.Endpoints.CopyTo (endPoints, 0);
-                       var builder = new DispatcherBuilder ();
+                       var builder = new DispatcherBuilder (this);
                        foreach (ServiceEndpoint se in endPoints) {
 
                                var commonParams = new BindingParameterCollection ();
@@ -587,9 +590,18 @@ namespace System.ServiceModel
        /// <remarks>Will re-use ChannelDispatchers when two endpoint uris are the same</remarks>
        partial class DispatcherBuilder
        {
+               ServiceHostBase host;
+
+               public DispatcherBuilder (ServiceHostBase host)
+               {
+                       this.host = host;
+               }
+
                List<ChannelDispatcher> built_dispatchers = new List<ChannelDispatcher> ();
                Dictionary<ServiceEndpoint, EndpointDispatcher> ep_to_dispatcher_ep = new Dictionary<ServiceEndpoint, EndpointDispatcher> ();
-               
+
+               internal static Action<ChannelDispatcher> ChannelDispatcherSetter;
+
                internal ChannelDispatcher BuildChannelDispatcher (Type serviceType, ServiceEndpoint se, BindingParameterCollection commonParams)
                {
                        //Let all behaviors add their binding parameters
@@ -602,9 +614,16 @@ namespace System.ServiceModel
                                ep = cd.InitializeServiceEndpoint (serviceType, se);
                        } else {
                                // Use the binding parameters to build the channel listener and Dispatcher.
-                               IChannelListener lf = BuildListener (se, commonParams);
-                               cd = new ChannelDispatcher (
-                                       lf, se.Binding.Name);
+                               lock (HttpTransportBindingElement.ListenerBuildLock) {
+                                       ServiceHostBase.CurrentServiceHostHack = host;
+                                       IChannelListener lf = BuildListener (se, commonParams);
+                                       cd = new ChannelDispatcher (lf, se.Binding.Name);
+                                       if (ChannelDispatcherSetter != null) {
+                                               ChannelDispatcherSetter (cd);
+                                               ChannelDispatcherSetter = null;
+                                       }
+                                       ServiceHostBase.CurrentServiceHostHack = null;
+                               }
                                ep = cd.InitializeServiceEndpoint (serviceType, se);
                                built_dispatchers.Add (cd);
                        }