BasicHttpBinding.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 / BasicHttpBinding.cs / 1 / BasicHttpBinding.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel
{ 
    using System;
    using System.Text; 
    using System.Collections.Generic; 
    using System.Collections.ObjectModel;
    using System.Configuration; 
    using System.Globalization;
    using System.Net;
    using System.Net.Security;
    using System.Runtime.Serialization; 
    using System.Security.Principal;
    using System.ServiceModel.Channels; 
    using System.ServiceModel.Configuration; 
    using System.ServiceModel.Security;
 
    using System.Xml;
    using System.ServiceModel.Security.Tokens;

    public class BasicHttpBinding : Binding, IBindingRuntimePreferences 
    {
        WSMessageEncoding messageEncoding; 
        // private BindingElements 
        HttpTransportBindingElement httpTransport;
        HttpsTransportBindingElement httpsTransport; 
        TextMessageEncodingBindingElement textEncoding;
        MtomMessageEncodingBindingElement mtomEncoding;
        BasicHttpSecurity security = new BasicHttpSecurity();
 
        public BasicHttpBinding() : this(BasicHttpSecurityMode.None) { }
 
        public BasicHttpBinding(string configurationName) : this() { ApplyConfiguration(configurationName); } 

        public BasicHttpBinding(BasicHttpSecurityMode securityMode) : base() 
        {
            Initialize();
            this.security.Mode = securityMode;
        } 

        BasicHttpBinding(BasicHttpSecurity security) : base() 
        { 
            Initialize();
            this.security = security; 
        }

        public bool AllowCookies
        { 
            get { return httpTransport.AllowCookies; }
            set 
            { 
                httpTransport.AllowCookies = value;
                httpsTransport.AllowCookies = value; 
            }
        }

        public bool BypassProxyOnLocal 
        {
            get { return httpTransport.BypassProxyOnLocal; } 
            set 
            {
                httpTransport.BypassProxyOnLocal = value; 
                httpsTransport.BypassProxyOnLocal = value;
            }
        }
 
        public HostNameComparisonMode HostNameComparisonMode
        { 
            get { return httpTransport.HostNameComparisonMode; } 
            set
            { 
                httpTransport.HostNameComparisonMode = value;
                httpsTransport.HostNameComparisonMode = value;
            }
        } 

        public int MaxBufferSize 
        { 
            get { return httpTransport.MaxBufferSize; }
            set 
            {
                httpTransport.MaxBufferSize = value;
                httpsTransport.MaxBufferSize = value;
                mtomEncoding.MaxBufferSize = value; 
            }
        } 
 
        public long MaxBufferPoolSize
        { 
            get { return httpTransport.MaxBufferPoolSize; }
            set
            {
                httpTransport.MaxBufferPoolSize = value; 
                httpsTransport.MaxBufferPoolSize = value;
            } 
        } 

        public long MaxReceivedMessageSize 
        {
            get { return httpTransport.MaxReceivedMessageSize; }
            set
            { 
                httpTransport.MaxReceivedMessageSize = value;
                httpsTransport.MaxReceivedMessageSize = value; 
            } 
        }
 
        public WSMessageEncoding MessageEncoding
        {
            get { return messageEncoding; }
            set { messageEncoding = value; } 
        }
 
        public Uri ProxyAddress 
        {
            get { return httpTransport.ProxyAddress; } 
            set
            {
                httpTransport.ProxyAddress = value;
                httpsTransport.ProxyAddress = value; 
            }
        } 
 
        public XmlDictionaryReaderQuotas ReaderQuotas
        { 
            get { return textEncoding.ReaderQuotas; }
            set
            {
                if (value == null) 
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
                value.CopyTo(textEncoding.ReaderQuotas); 
                value.CopyTo(mtomEncoding.ReaderQuotas); 
            }
        } 

        public override string Scheme { get { return GetTransport().Scheme; } }

        public BasicHttpSecurity Security 
        {
            get { return this.security; } 
        } 

        public EnvelopeVersion EnvelopeVersion 
        {
            get { return EnvelopeVersion.Soap11; }
        }
 
        public System.Text.Encoding TextEncoding
        { 
            get { return textEncoding.WriteEncoding; } 
            set
            { 
                textEncoding.WriteEncoding = value;
                mtomEncoding.WriteEncoding = value;
            }
        } 

        public TransferMode TransferMode 
        { 
            get { return httpTransport.TransferMode; }
            set 
            {
                httpTransport.TransferMode = value;
                httpsTransport.TransferMode = value;
            } 
        }
 
        public bool UseDefaultWebProxy 
        {
            get { return httpTransport.UseDefaultWebProxy; } 
            set
            {
                httpTransport.UseDefaultWebProxy = value;
                httpsTransport.UseDefaultWebProxy = value; 
            }
        } 
 
        bool IBindingRuntimePreferences.ReceiveSynchronously
        { 
            get { return false; }
        }

        void Initialize() 
        {
            httpTransport = new HttpTransportBindingElement(); 
            httpsTransport = new HttpsTransportBindingElement(); 
            messageEncoding = WSMessageEncoding.Text;
            textEncoding = new TextMessageEncodingBindingElement(); 
            textEncoding.MessageVersion = MessageVersion.Soap11;
            mtomEncoding = new MtomMessageEncodingBindingElement();
            mtomEncoding.MessageVersion = MessageVersion.Soap11;
        } 

        // check that properties of the HttpTransportBindingElement and 
        // MessageEncodingBindingElement not exposed as properties on BasicHttpBinding 
        // match default values of the binding elements
        bool IsBindingElementsMatch(HttpTransportBindingElement transport, MessageEncodingBindingElement encoding) 
        {
            if (this.MessageEncoding == WSMessageEncoding.Text)
            {
                if (!this.textEncoding.IsMatch(encoding)) 
                    return false;
            } 
            else if (this.MessageEncoding == WSMessageEncoding.Mtom) 
            {
                if (!this.mtomEncoding.IsMatch(encoding)) 
                    return false;
            }
            if (!this.GetTransport().IsMatch(transport))
                return false; 

            return true; 
        } 

        void InitializeFrom(HttpTransportBindingElement transport, MessageEncodingBindingElement encoding) 
        {
            this.BypassProxyOnLocal = transport.BypassProxyOnLocal;
            this.HostNameComparisonMode = transport.HostNameComparisonMode;
            this.MaxBufferPoolSize = transport.MaxBufferPoolSize; 
            this.MaxBufferSize = transport.MaxBufferSize;
            this.MaxReceivedMessageSize = transport.MaxReceivedMessageSize; 
            this.ProxyAddress = transport.ProxyAddress; 
            this.TransferMode = transport.TransferMode;
            this.UseDefaultWebProxy = transport.UseDefaultWebProxy; 

            // BasicHttpBinding only supports Text and Mtom encoding
            if (encoding is TextMessageEncodingBindingElement)
            { 
                this.MessageEncoding = WSMessageEncoding.Text;
                TextMessageEncodingBindingElement text = (TextMessageEncodingBindingElement)encoding; 
                this.TextEncoding = text.WriteEncoding; 
                this.ReaderQuotas = text.ReaderQuotas;
 

            }
            else if (encoding is MtomMessageEncodingBindingElement)
            { 
                messageEncoding = WSMessageEncoding.Mtom;
                MtomMessageEncodingBindingElement mtom = (MtomMessageEncodingBindingElement)encoding; 
                this.TextEncoding = mtom.WriteEncoding; 
                this.ReaderQuotas = mtom.ReaderQuotas;
            } 
        }

        void ApplyConfiguration(string configurationName)
        { 
            BasicHttpBindingCollectionElement section = BasicHttpBindingCollectionElement.GetBindingCollectionElement();
            BasicHttpBindingElement element = section.Bindings[configurationName]; 
            if (element == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException( 
                    SR.GetString(SR.ConfigInvalidBindingConfigurationName,
                                 configurationName,
                                 ConfigurationStrings.BasicHttpBindingCollectionElementName)));
            } 
            else
            { 
                element.ApplyConfiguration(this); 
            }
        } 

        public override BindingElementCollection CreateBindingElements()
        {   // return collection of BindingElements
            BindingElementCollection bindingElements = new BindingElementCollection(); 
            // order of BindingElements is important
            // add security (*optional) 
            SecurityBindingElement wsSecurity = this.CreateMessageSecurity(); 
            if (wsSecurity != null)
            { 
                bindingElements.Add(wsSecurity);
            }
            // add encoding (text or mtom)
            WSMessageEncodingHelper.SyncUpEncodingBindingElementProperties(textEncoding, mtomEncoding); 
            if (this.MessageEncoding == WSMessageEncoding.Text)
                bindingElements.Add(textEncoding); 
            else if (this.MessageEncoding == WSMessageEncoding.Mtom) 
                bindingElements.Add(mtomEncoding);
            // add transport (http or https) 
            bindingElements.Add(GetTransport());

            return bindingElements.Clone();
        } 

        internal static bool TryCreate(BindingElementCollection elements, out Binding binding) 
        { 
            binding = null;
            if (elements.Count > 3) 
                return false;

            SecurityBindingElement securityElement = null;
            MessageEncodingBindingElement encoding = null; 
            HttpTransportBindingElement transport = null;
 
            foreach(BindingElement element in elements) 
            {
                if (element is SecurityBindingElement) 
                    securityElement = element as SecurityBindingElement;
                else if (element is TransportBindingElement)
                    transport = element as HttpTransportBindingElement;
                else if (element is MessageEncodingBindingElement) 
                    encoding = element as MessageEncodingBindingElement;
                else 
                    return false; 
            }
 
            // process transport binding element
            UnifiedSecurityMode mode;
            HttpTransportSecurity transportSecurity = new HttpTransportSecurity();
            if (!GetSecurityModeFromTransport(transport, transportSecurity, out mode)) 
                return false;
            if (encoding == null) 
                return false; 
            // BasicHttpBinding only supports Soap11
            if (!encoding.CheckEncodingVersion(EnvelopeVersion.Soap11)) 
                return false;

            BasicHttpSecurity security;
            if (!TryCreateSecurity(securityElement, mode, transportSecurity, out security)) 
                return false;
 
            BasicHttpBinding basicHttpBinding = new BasicHttpBinding(security); 
            basicHttpBinding.InitializeFrom(transport, encoding);
 
            // make sure all our defaults match
            if (!basicHttpBinding.IsBindingElementsMatch(transport, encoding))
                return false;
 
            binding = basicHttpBinding;
            return true; 
        } 

        TransportBindingElement GetTransport() 
        {
            if (security.Mode == BasicHttpSecurityMode.Transport || security.Mode == BasicHttpSecurityMode.TransportWithMessageCredential)
            {
                security.EnableTransportSecurity(httpsTransport); 
                return httpsTransport;
            } 
            else if (security.Mode == BasicHttpSecurityMode.TransportCredentialOnly) 
            {
                security.EnableTransportAuthentication(httpTransport); 
                return httpTransport;
            }
            else
            { 
                // ensure that there is no transport security
                security.DisableTransportAuthentication(httpTransport); 
                return httpTransport; 
            }
        } 

        static bool GetSecurityModeFromTransport(HttpTransportBindingElement http, HttpTransportSecurity transportSecurity, out UnifiedSecurityMode mode)
        {
            mode = UnifiedSecurityMode.None; 
            if (http == null)
                return false; 
 
            if (http is HttpsTransportBindingElement)
            { 
                mode = UnifiedSecurityMode.Transport | UnifiedSecurityMode.TransportWithMessageCredential;
                BasicHttpSecurity.EnableTransportSecurity((HttpsTransportBindingElement)http, transportSecurity);
            }
            else if (HttpTransportSecurity.IsDisabledTransportAuthentication(http)) 
            {
                mode = UnifiedSecurityMode.Message | UnifiedSecurityMode.None; 
            } 
            else if (!BasicHttpSecurity.IsEnabledTransportAuthentication(http, transportSecurity))
            { 
                return false;
            }
            else
            { 
                mode = UnifiedSecurityMode.TransportCredentialOnly;
            } 
            return true; 
        }
 
        SecurityBindingElement CreateMessageSecurity()
        {
            return security.CreateMessageSecurity();
        } 

        static bool TryCreateSecurity(SecurityBindingElement securityElement, UnifiedSecurityMode mode, HttpTransportSecurity transportSecurity, out BasicHttpSecurity security) 
        { 
            return BasicHttpSecurity.TryCreate(securityElement, mode, transportSecurity, out security);
        } 
    }
}

// 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