protected internal override object CreateBehavior()
 {
     UseRequestHeadersForMetadataAddressBehavior behavior = new UseRequestHeadersForMetadataAddressBehavior();
     foreach (DefaultPortElement DefaultPort in this.DefaultPorts)
     {
         behavior.DefaultPortsByScheme.Add(DefaultPort.Scheme, DefaultPort.Port);
     }
     return behavior;
 }
        /// <summary>
        /// Creates a ServiceHost for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The Array of type Uri that contains the base addresses for the service hosted.</param>
        /// <returns>A ServiceHost for the type of service specified with a specific base address.</returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            ServiceHost customServiceHost = base.CreateServiceHost(serviceType, baseAddresses);

            UseRequestHeadersForMetadataAddressBehavior loadBalancedBehavior =
                customServiceHost.Description.Behaviors.Find<UseRequestHeadersForMetadataAddressBehavior>();
            if (null == loadBalancedBehavior)
            {
                loadBalancedBehavior = new UseRequestHeadersForMetadataAddressBehavior();
                loadBalancedBehavior.DefaultPortsByScheme.Add("http", 80);
                loadBalancedBehavior.DefaultPortsByScheme.Add("https", 443);
                customServiceHost.Description.Behaviors.Add(loadBalancedBehavior);
            }

            return customServiceHost;
        }
        void SetExtensionProperties(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex)
        {
            mex.ExternalMetadataLocation = this.ExternalMetadataLocation;
            mex.Initializer     = new MetadataExtensionInitializer(this, description, host);
            mex.HttpGetEnabled  = this.httpGetEnabled;
            mex.HttpsGetEnabled = this.httpsGetEnabled;

            mex.HttpGetUrl  = host.GetVia(Uri.UriSchemeHttp, this.httpGetUrl == null ? new Uri(string.Empty, UriKind.Relative) : this.httpGetUrl);
            mex.HttpsGetUrl = host.GetVia(Uri.UriSchemeHttps, this.httpsGetUrl == null ? new Uri(string.Empty, UriKind.Relative) : this.httpsGetUrl);

            mex.HttpGetBinding  = this.httpGetBinding;
            mex.HttpsGetBinding = this.httpsGetBinding;

            UseRequestHeadersForMetadataAddressBehavior dynamicUpdateBehavior = description.Behaviors.Find <UseRequestHeadersForMetadataAddressBehavior>();

            if (dynamicUpdateBehavior != null)
            {
                mex.UpdateAddressDynamically = true;
                mex.UpdatePortsByScheme      = new Dictionary <string, int>(dynamicUpdateBehavior.DefaultPortsByScheme);
            }

            foreach (ChannelDispatcherBase dispatcherBase in host.ChannelDispatchers)
            {
                ChannelDispatcher dispatcher = dispatcherBase as ChannelDispatcher;
                if (dispatcher != null && IsMetadataTransferDispatcher(description, dispatcher))
                {
                    mex.MexEnabled = true;
                    mex.MexUrl     = dispatcher.Listener.Uri;
                    if (dynamicUpdateBehavior != null)
                    {
                        foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
                        {
                            if (!endpointDispatcher.AddressFilterSetExplicit)
                            {
                                endpointDispatcher.AddressFilter = new MatchAllMessageFilter();
                            }
                        }
                    }
                    break;
                }
            }
        }
Esempio n. 4
0
        // Start WCF Service to manage VM
        private IVMManager StartVMManagerService()
        {
            try
            {
                // If available, get SSL certificate from service configuration
                string sslCertificateSHA1Thumbprint = null;
                try
                {
                    sslCertificateSHA1Thumbprint = RoleEnvironment.GetConfigurationSettingValue("SSLCertificateSHA1Thumbprint");
                }
                catch (Exception)
                {
                    // Ignore, it means SSLCertificateSHA1Thumbprint is not defined
                }

                // Get VMManager WCF service binding
                BasicHttpBinding binding = WindowsAzureVMManager.GetVMManagerServiceBinding(sslCertificateSHA1Thumbprint);                
                wcfVMManagerServiceHost = new ServiceHost(typeof(WindowsAzureVMManager));
                wcfVMManagerServiceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
                wcfVMManagerServiceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new WindowsAzureVMManagerUsernamePasswordValidator();

                UseRequestHeadersForMetadataAddressBehavior requestHeaderBehavior = new UseRequestHeadersForMetadataAddressBehavior();
                IPEndPoint ep = WindowsAzureVMManager.GetVMManagerServiceEndpoint(sslCertificateSHA1Thumbprint);  
                if (string.IsNullOrEmpty(sslCertificateSHA1Thumbprint))
                {
                    requestHeaderBehavior.DefaultPortsByScheme.Add("http", int.Parse(ep.Port.ToString()));
                }
                else
                {
                    requestHeaderBehavior.DefaultPortsByScheme.Add("https", int.Parse(ep.Port.ToString()));
                    wcfVMManagerServiceHost.Credentials.ServiceCertificate.SetCertificate(
                        StoreLocation.LocalMachine,
                        StoreName.My,
                        X509FindType.FindByThumbprint,
                        RoleEnvironment.GetConfigurationSettingValue("SSLCertificateSHA1Thumbprint")
                    );
                }
                wcfVMManagerServiceHost.Description.Behaviors.Add(requestHeaderBehavior);                

                // Enable WCF service debugging
                ServiceDebugBehavior sdb = wcfVMManagerServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
                if (sdb == null)
                {
                    sdb = new ServiceDebugBehavior();
                    wcfVMManagerServiceHost.Description.Behaviors.Add(sdb);
                }
                sdb.IncludeExceptionDetailInFaults = true;

                string endpoint = WindowsAzureVMManager.GetVMManagerServiceEndpointAddress(sslCertificateSHA1Thumbprint);
                wcfVMManagerServiceHost.AddServiceEndpoint(typeof(IVMManager), binding, endpoint);
                
                ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();                
                if (string.IsNullOrEmpty(sslCertificateSHA1Thumbprint))
                {
                    metadataBehavior.HttpGetEnabled = true;
                    metadataBehavior.HttpGetUrl = new Uri(endpoint);
                }
                else
                {
                    metadataBehavior.HttpsGetEnabled = true;
                    metadataBehavior.HttpsGetUrl = new Uri(endpoint);
                }                   
                                
                wcfVMManagerServiceHost.Description.Behaviors.Add(metadataBehavior);

                wcfVMManagerServiceHost.Open();
                Trace.TraceInformation("External WCF Service started on endpoint: {0}", endpoint);                
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                Trace.TraceError(ex.StackTrace);
            }

            // Return service instance
            return WindowsAzureVMManager.GetVMManager();
        }