HostedTransportConfigurationManager.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Activation / HostedTransportConfigurationManager.cs / 1 / HostedTransportConfigurationManager.cs

                            //---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------

namespace System.ServiceModel.Activation 
{
    using System.Collections; 
    using System.Collections.Generic; 
    using System.ServiceModel;
    using System.ServiceModel.Channels; 
    using System.ServiceModel.Diagnostics;
    using System.Diagnostics;
    using System.Reflection;
    using System.Globalization; 
    using System.Runtime.Serialization;
    using System.ServiceModel.Configuration; 
    using System.Security; 
    using System.Security.Permissions;
 
    class HostedTransportConfigurationManager
    {
        IDictionary configurations = new Dictionary(StringComparer.Ordinal);
        bool initialized = false; 
        MetabaseSettings metabaseSettings;
        static HostedTransportConfigurationManager singleton; 
        static object syncRoot = new object(); 
  #pragma warning disable 436
        const string WasHostingAssemblyName = "System.ServiceModel.WasHosting, Version=" + ThisAssembly.Version + ", Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKey; 
        const string MetabaseSettingsIis7FactoryTypeName = "System.ServiceModel.WasHosting.MetabaseSettingsIis7Factory, " + WasHostingAssemblyName;
  #pragma warning restore 436
        const string CreateMetabaseSettingsIis7MethodName = "CreateMetabaseSettings";
 
        HostedTransportConfigurationManager()
        { 
            if (!Iis7Helper.IsIis7) 
            {
                metabaseSettings = new MetabaseSettingsIis6(); 
            }
            else
            {
 
                metabaseSettings = CreateWasHostingMetabaseSettings();
            } 
        } 

        HostedTransportConfigurationManager(MetabaseSettings metabaseSettings) 
        {
            this.metabaseSettings = metabaseSettings;
        }
 
        /// 
        /// Critical - Calls Critical methods CreateMetabaseSettings 
        /// Safe     - Ensures that only the correct, well-known method is called to construct the metabase settings 
        ///            no other details are leaked, and no control flow or data is allowed in.
        ///  
        [SecurityCritical, SecurityTreatAsSafe]
        static MetabaseSettingsIis CreateWasHostingMetabaseSettings()
        {
            Type type = Type.GetType(MetabaseSettingsIis7FactoryTypeName, false); 
            if (type == null)
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_MetabaseSettingsIis7TypeNotFound, MetabaseSettingsIis7FactoryTypeName, WasHostingAssemblyName))); 
            }
            return CreateMetabaseSettings(type); 
        }

        /// 
        /// Critical - Asserts ReflectionPermission(MemberAccess) in order to call a well-known internal static in WasHosting.dll 
        ///            caller must ensure that 'type' argument refers to the trusted, well-known Type
        ///  
        [SecurityCritical] 
        static MetabaseSettingsIis CreateMetabaseSettings(Type type)
        { 
            object instance = null;
            MethodInfo method = type.GetMethod(CreateMetabaseSettingsIis7MethodName, BindingFlags.NonPublic | BindingFlags.Static);

            try 
            {
                new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert(); 
 
                instance = method.Invoke(null, null);
            } 
            finally
            {
                ReflectionPermission.RevertAssert();
            } 

            if (!(instance is MetabaseSettingsIis)) 
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_BadMetabaseSettingsIis7Type, type.AssemblyQualifiedName)));
            } 

            return (MetabaseSettingsIis)instance;
        }
 
        internal static void EnsureInitializedForSimpleApplicationHost(HostedHttpRequestAsyncResult result)
        { 
            if (singleton != null) 
            {
                return; 
            }

            lock (syncRoot)
            { 
                if (singleton != null)
                    return; 
 
                singleton = new HostedTransportConfigurationManager(new MetabaseSettingsCassini(result));
            } 
        }

        internal static MetabaseSettings MetabaseSettings
        { 
            get
            { 
                return HostedTransportConfigurationManager.Value.metabaseSettings; 
            }
        } 

        object ThisLock
        {
            get 
            {
                return this; 
            } 
        }
 
        static HostedTransportConfigurationManager Value
        {
            get
            { 
                if (singleton == null)
                { 
                    lock (syncRoot) 
                    {
                        if (singleton == null) 
                        {
                            ServiceHostingEnvironment.EnsureInitialized();
                            singleton = new HostedTransportConfigurationManager();
                        } 
                    }
                } 
                return singleton; 
            }
        } 

        void EnsureInitialized()
        {
            if (!initialized) 
            {
                lock (ThisLock) 
                { 
                    if (!initialized)
                    { 
                        // Register protocols.
                        foreach (string protocol in metabaseSettings.GetProtocols())
                        {
                            // special case HTTP, it's a legacy protocol 
                            if (string.CompareOrdinal(protocol, Uri.UriSchemeHttp) == 0 ||
                                string.CompareOrdinal(protocol, Uri.UriSchemeHttps) == 0) 
                            { 
                                HttpHostedTransportConfiguration httpConfiguration = null;
                                if (string.CompareOrdinal(protocol, Uri.UriSchemeHttp) == 0) 
                                {
                                    httpConfiguration = new HttpHostedTransportConfiguration();
                                }
                                else 
                                {
                                    httpConfiguration = new HttpsHostedTransportConfiguration(); 
                                } 

                                configurations.Add(protocol, httpConfiguration); 
                            }
                            else
                            {
                                if (!Iis7Helper.IsIis7) 
                                {
                                    DiagnosticUtility.DebugAssert("HostedTransportConfigurationManager.EnsureInitialized() protocols other than http and https can only be configured in IIS7"); 
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(true); 
                                }
                                if (PartialTrustHelpers.NeedPartialTrustInvoke) 
                                {
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.PartialTrustNonHttpActivation, protocol, HostingEnvironmentWrapper.ApplicationVirtualPath)));
                                }
                                AddHostedTransportConfigurationIis7(protocol); 
                            }
                        } 
 
                        initialized = true;
                    } 
                }
            }
        }
 
        void AddHostedTransportConfigurationIis7(string protocol)
        { 
            HostedTransportConfiguration configuration = null; 
            try
            { 
                ServiceHostingEnvironmentSection section = ServiceHostingEnvironmentSection.GetSection();
                if (section.TransportConfigurationTypes.ContainsKey(protocol))
                {
                    TransportConfigurationTypeElement element = section.TransportConfigurationTypes[protocol]; 
                    Debug.Print("HostedTransportConfigurationManager.AddHostedTransportConfigurationIis7() found TransportConfigurationTypes for protocol: " + protocol + " name: " + element.TransportConfigurationType);
 
                    Type type = Type.GetType(element.TransportConfigurationType); 
                    configuration = Activator.CreateInstance(type) as HostedTransportConfiguration;
                    configurations.Add(protocol, configuration); 
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_ProtocolNoConfiguration, protocol))); 
                }
            } 
            catch (Exception exception) 
            {
                if (!(DiagnosticUtility.IsFatal(exception))) 
                {
                    Debug.Print("HostedTransportConfigurationManager.AddHostedTransportConfigurationIis7() caught exception: " + exception);
                    if (DiagnosticUtility.ShouldTraceError)
                    { 
                        TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.WebHostProtocolMisconfigured,
                            new StringTraceRecord("Protocol", protocol), 
                            this, exception); 
                    }
                } 
                throw;
            }
        }
 
        internal static Uri[] GetBaseAddresses(string virtualPath)
        { 
            return HostedTransportConfigurationManager.Value.InternalGetBaseAddresses(virtualPath); 
        }
 
        internal static HostedTransportConfiguration GetConfiguration(string scheme)
        {
            return HostedTransportConfigurationManager.Value.InternalGetConfiguration(scheme);
        } 

        Uri[] InternalGetBaseAddresses(string virtualPath) 
        { 
            EnsureInitialized();
            List baseAddresses = new List(); 
            foreach (HostedTransportConfiguration configuration in configurations.Values)
            {
                baseAddresses.AddRange(configuration.GetBaseAddresses(virtualPath));
            } 

            return baseAddresses.ToArray(); 
        } 

        HostedTransportConfiguration InternalGetConfiguration(string scheme) 
        {
            EnsureInitialized();
            if (!configurations.ContainsKey(scheme))
            { 
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.Hosting_NotSupportedProtocol, scheme)));
            } 
 
            return configurations[scheme];
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK