PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop/Metro/MetroBinding.cs

#
C# | 107 lines | 66 code | 13 blank | 28 comment | 3 complexity | c012698e69d86bc1e6734c97b58d1e6e MD5 | raw file
  1// <copyright file="MetroBinding.cs" company="Microsoft Corporation">
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ServiceModel.Interop.Metro
  6{
  7    using System;
  8    using System.Configuration;
  9    using System.Globalization;
 10    using System.ServiceModel;
 11    using System.ServiceModel.Channels;
 12    using System.ServiceModel.Configuration;
 13    
 14    using Microsoft.ServiceModel.Interop.Metro.Configuration;
 15    using Microsoft.ServiceModel.Interop.Properties;
 16
 17    /// <summary>
 18    /// WCF Binding implementation for Metro
 19    /// </summary>
 20    public class MetroBinding : InteropBinding, IBindingRuntimePreferences
 21    {
 22        private MetroReliableSession innerReliableSession;
 23        private MetroSecurity innerSecurity;
 24
 25        /// <summary>
 26        /// Initializes a new instance of the MetroBinding class
 27        /// </summary>
 28        public MetroBinding()
 29            : this(MetroSecurityMode.UserNameOverCertificate)
 30        {
 31        }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the MetroBinding class
 35        /// </summary>
 36        /// <param name="configName">Binding configuration name</param>
 37        public MetroBinding(string configName)
 38        {
 39            if (string.IsNullOrEmpty(configName))
 40            {
 41                throw new ArgumentNullException("configName");
 42            }
 43
 44            this.innerSecurity = new MetroSecurity(this.InnerBinding.Security, MetroSecurityMode.UserNameOverCertificate);
 45            this.innerReliableSession = new MetroReliableSession(this.InnerBinding.ReliableSession);
 46
 47            this.ApplyConfiguration(configName);
 48        }
 49
 50        /// <summary>
 51        /// Initializes a new instance of the MetroBinding class
 52        /// </summary>
 53        /// <param name="securityMode">Security Mode</param>
 54        public MetroBinding(MetroSecurityMode securityMode)
 55            : this(securityMode, false)
 56        {
 57        }
 58
 59        /// <summary>
 60        /// Initializes a new instance of the MetroBinding class
 61        /// </summary>
 62        /// <param name="securityMode">Security mode</param>
 63        /// <param name="reliableSessionEnabled">A boolean value for specifing whehter reliable session is enabled</param>
 64        public MetroBinding(MetroSecurityMode securityMode, bool reliableSessionEnabled)
 65            : base(new WS2007HttpBinding(SecurityMode.Message, reliableSessionEnabled))
 66        {
 67            this.innerReliableSession = new MetroReliableSession(InnerBinding.ReliableSession);
 68            this.innerSecurity = new MetroSecurity(InnerBinding.Security, securityMode);
 69            this.innerSecurity.EstablishSecurityContext = true;
 70        }
 71
 72        /// <summary>
 73        /// Gets or sets the security binding element
 74        /// </summary>
 75        public MetroSecurity Security
 76        {
 77            get { return this.innerSecurity; }
 78            set { this.innerSecurity = value; }
 79        }
 80
 81        /// <summary>
 82        /// Gets or sets the reliable session binding element
 83        /// </summary>
 84        public MetroReliableSession ReliableSession
 85        {
 86            get { return this.innerReliableSession; }
 87            set { this.innerReliableSession = value; }
 88        }
 89
 90        private void ApplyConfiguration(string configurationName)
 91        {
 92            BindingsSection bindings = (BindingsSection)ConfigurationManager.GetSection("system.serviceModel/bindings");
 93            MetroBindingCollectionElement section = (MetroBindingCollectionElement)bindings["metroBinding"];
 94            MetroBindingElement element = section.Bindings[configurationName];
 95            if (element == null)
 96            {
 97                throw new System.Configuration.ConfigurationErrorsException(
 98                    string.Format(CultureInfo.CurrentCulture, Strings.Binding_Not_Found, configurationName, section.BindingName));
 99            }
100            else
101            {
102                element.ApplyConfiguration(this);
103            }
104        }
105    }
106}
107