NetTcpContextBinding.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.Net.Security;
  9. using System.Runtime.CompilerServices;
  10. using System.ServiceModel.Channels;
  11. using System.ServiceModel.Configuration;
  12. using System.ServiceModel.Security;
  13. [TypeForwardedFrom("System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  14. public class NetTcpContextBinding : NetTcpBinding
  15. {
  16. bool contextManagementEnabled = ContextBindingElement.DefaultContextManagementEnabled;
  17. ProtectionLevel contextProtectionLevel = ContextBindingElement.DefaultProtectionLevel;
  18. public NetTcpContextBinding()
  19. : base()
  20. {
  21. }
  22. public NetTcpContextBinding(SecurityMode securityMode)
  23. : base(securityMode)
  24. {
  25. }
  26. public NetTcpContextBinding(string configName)
  27. : base()
  28. {
  29. if (configName == null)
  30. {
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("configName");
  32. }
  33. this.ApplyConfiguration(configName);
  34. }
  35. public NetTcpContextBinding(SecurityMode securityMode, bool reliableSessionEnabled)
  36. : base(securityMode, reliableSessionEnabled)
  37. {
  38. }
  39. NetTcpContextBinding(NetTcpBinding netTcpBinding)
  40. {
  41. NetTcpContextBindingPropertyTransferHelper helper = new NetTcpContextBindingPropertyTransferHelper();
  42. helper.InitializeFrom(netTcpBinding);
  43. helper.SetBindingElementType(typeof(NetTcpContextBinding));
  44. helper.ApplyConfiguration(this);
  45. }
  46. [DefaultValue(null)]
  47. public Uri ClientCallbackAddress
  48. {
  49. get;
  50. set;
  51. }
  52. [DefaultValue(ContextBindingElement.DefaultContextManagementEnabled)]
  53. public bool ContextManagementEnabled
  54. {
  55. get
  56. {
  57. return this.contextManagementEnabled;
  58. }
  59. set
  60. {
  61. this.contextManagementEnabled = value;
  62. }
  63. }
  64. [DefaultValue(ContextBindingElement.DefaultProtectionLevel)]
  65. public ProtectionLevel ContextProtectionLevel
  66. {
  67. get
  68. {
  69. return this.contextProtectionLevel;
  70. }
  71. set
  72. {
  73. if (!ProtectionLevelHelper.IsDefined(value))
  74. {
  75. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  76. }
  77. this.contextProtectionLevel = value;
  78. }
  79. }
  80. public override BindingElementCollection CreateBindingElements()
  81. {
  82. BindingElementCollection result = base.CreateBindingElements();
  83. result.Insert(0, new ContextBindingElement(this.ContextProtectionLevel, ContextExchangeMechanism.ContextSoapHeader, this.ClientCallbackAddress, this.ContextManagementEnabled));
  84. return result;
  85. }
  86. internal static new bool TryCreate(BindingElementCollection bindingElements, out Binding binding)
  87. {
  88. if (bindingElements == null)
  89. {
  90. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingElements");
  91. }
  92. binding = null;
  93. ContextBindingElement contextBindingElement = bindingElements.Find<ContextBindingElement>();
  94. if (contextBindingElement != null && contextBindingElement.ContextExchangeMechanism != ContextExchangeMechanism.HttpCookie)
  95. {
  96. BindingElementCollection bindingElementsWithoutContext = new BindingElementCollection(bindingElements);
  97. bindingElementsWithoutContext.Remove<ContextBindingElement>();
  98. Binding netTcpBinding;
  99. if (NetTcpBinding.TryCreate(bindingElementsWithoutContext, out netTcpBinding))
  100. {
  101. NetTcpContextBinding contextBinding = new NetTcpContextBinding((NetTcpBinding)netTcpBinding);
  102. contextBinding.ContextProtectionLevel = contextBindingElement.ProtectionLevel;
  103. contextBinding.ContextManagementEnabled = contextBindingElement.ContextManagementEnabled;
  104. binding = contextBinding;
  105. }
  106. }
  107. return binding != null;
  108. }
  109. void ApplyConfiguration(string configurationName)
  110. {
  111. NetTcpContextBindingCollectionElement section = NetTcpContextBindingCollectionElement.GetBindingCollectionElement();
  112. NetTcpContextBindingElement element = section.Bindings[configurationName];
  113. element.ApplyConfiguration(this);
  114. }
  115. class NetTcpContextBindingPropertyTransferHelper : NetTcpBindingElement
  116. {
  117. Type bindingElementType = typeof(NetTcpBinding);
  118. protected override Type BindingElementType
  119. {
  120. get
  121. {
  122. return this.bindingElementType;
  123. }
  124. }
  125. public void SetBindingElementType(Type bindingElementType)
  126. {
  127. this.bindingElementType = bindingElementType;
  128. }
  129. }
  130. }
  131. }