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