StandardBindingElement.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. namespace System.ServiceModel.Configuration
  5. {
  6. using System.ComponentModel;
  7. using System.Configuration;
  8. using System.Runtime;
  9. using System.Security;
  10. using System.ServiceModel;
  11. using System.ServiceModel.Channels;
  12. public abstract partial class StandardBindingElement : ServiceModelConfigurationElement, IBindingConfigurationElement, IConfigurationContextProviderInternal
  13. {
  14. [Fx.Tag.SecurityNote(Critical = "Stores information used in a security decision.")]
  15. [SecurityCritical]
  16. EvaluationContextHelper contextHelper;
  17. protected StandardBindingElement()
  18. : this(null)
  19. {
  20. }
  21. protected StandardBindingElement(string name)
  22. {
  23. if (!String.IsNullOrEmpty(name))
  24. {
  25. this.Name = name;
  26. }
  27. }
  28. protected abstract Type BindingElementType
  29. {
  30. get;
  31. }
  32. [ConfigurationProperty(ConfigurationStrings.Name, Options = ConfigurationPropertyOptions.IsKey)]
  33. [StringValidator(MinLength = 0)]
  34. public string Name
  35. {
  36. get { return (string)base[ConfigurationStrings.Name]; }
  37. set
  38. {
  39. if (String.IsNullOrEmpty(value))
  40. {
  41. value = String.Empty;
  42. }
  43. base[ConfigurationStrings.Name] = value;
  44. }
  45. }
  46. [ConfigurationProperty(ConfigurationStrings.CloseTimeout, DefaultValue = ServiceDefaults.CloseTimeoutString)]
  47. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  48. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
  49. public TimeSpan CloseTimeout
  50. {
  51. get { return (TimeSpan)base[ConfigurationStrings.CloseTimeout]; }
  52. set { base[ConfigurationStrings.CloseTimeout] = value; }
  53. }
  54. [ConfigurationProperty(ConfigurationStrings.OpenTimeout, DefaultValue = ServiceDefaults.OpenTimeoutString)]
  55. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  56. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
  57. public TimeSpan OpenTimeout
  58. {
  59. get { return (TimeSpan)base[ConfigurationStrings.OpenTimeout]; }
  60. set { base[ConfigurationStrings.OpenTimeout] = value; }
  61. }
  62. [ConfigurationProperty(ConfigurationStrings.ReceiveTimeout, DefaultValue = ServiceDefaults.ReceiveTimeoutString)]
  63. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  64. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
  65. public TimeSpan ReceiveTimeout
  66. {
  67. get { return (TimeSpan)base[ConfigurationStrings.ReceiveTimeout]; }
  68. set { base[ConfigurationStrings.ReceiveTimeout] = value; }
  69. }
  70. [ConfigurationProperty(ConfigurationStrings.SendTimeout, DefaultValue = ServiceDefaults.SendTimeoutString)]
  71. [TypeConverter(typeof(TimeSpanOrInfiniteConverter))]
  72. [ServiceModelTimeSpanValidator(MinValueString = ConfigurationStrings.TimeSpanZero)]
  73. public TimeSpan SendTimeout
  74. {
  75. get { return (TimeSpan)base[ConfigurationStrings.SendTimeout]; }
  76. set { base[ConfigurationStrings.SendTimeout] = value; }
  77. }
  78. public void ApplyConfiguration(Binding binding)
  79. {
  80. if (null == binding)
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  83. }
  84. if (binding.GetType() != this.BindingElementType)
  85. {
  86. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidTypeForBinding,
  87. (this.BindingElementType == null) ? string.Empty : this.BindingElementType.AssemblyQualifiedName,
  88. binding.GetType().AssemblyQualifiedName));
  89. }
  90. // The properties binding.Name and this.Name are actually two different things:
  91. // - binding.Name corresponds to how the WSDL for this binding is surfaced,
  92. // it is used in conjunction with binding.Namespace
  93. // - this.Name is a token used as a key in the binding collection to identify
  94. // a specific bucket of configuration settings.
  95. // Thus, the Name property is skipped here.
  96. binding.CloseTimeout = this.CloseTimeout;
  97. binding.OpenTimeout = this.OpenTimeout;
  98. binding.ReceiveTimeout = this.ReceiveTimeout;
  99. binding.SendTimeout = this.SendTimeout;
  100. this.OnApplyConfiguration(binding);
  101. }
  102. protected virtual internal void InitializeFrom(Binding binding)
  103. {
  104. if (null == binding)
  105. {
  106. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
  107. }
  108. if (binding.GetType() != this.BindingElementType)
  109. {
  110. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidTypeForBinding,
  111. (this.BindingElementType == null) ? string.Empty : this.BindingElementType.AssemblyQualifiedName,
  112. binding.GetType().AssemblyQualifiedName));
  113. }
  114. // The properties binding.Name and this.Name are actually two different things:
  115. // - binding.Name corresponds to how the WSDL for this binding is surfaced,
  116. // it is used in conjunction with binding.Namespace
  117. // - this.Name is a token used as a key in the binding collection to identify
  118. // a specific bucket of configuration settings.
  119. // Thus, the Name property is skipped here.
  120. SetPropertyValueIfNotDefaultValue(ConfigurationStrings.CloseTimeout, binding.CloseTimeout);
  121. SetPropertyValueIfNotDefaultValue(ConfigurationStrings.OpenTimeout, binding.OpenTimeout);
  122. SetPropertyValueIfNotDefaultValue(ConfigurationStrings.ReceiveTimeout, binding.ReceiveTimeout);
  123. SetPropertyValueIfNotDefaultValue(ConfigurationStrings.SendTimeout, binding.SendTimeout);
  124. }
  125. protected abstract void OnApplyConfiguration(Binding binding);
  126. [Fx.Tag.SecurityNote(Critical = "Accesses critical field contextHelper.")]
  127. [SecurityCritical]
  128. protected override void Reset(ConfigurationElement parentElement)
  129. {
  130. this.contextHelper.OnReset(parentElement);
  131. base.Reset(parentElement);
  132. }
  133. ContextInformation IConfigurationContextProviderInternal.GetEvaluationContext()
  134. {
  135. return this.EvaluationContext;
  136. }
  137. [Fx.Tag.SecurityNote(Critical = "Accesses critical field contextHelper.",
  138. Miscellaneous = "RequiresReview -- the return value will be used for a security decision -- see comment in interface definition.")]
  139. [SecurityCritical]
  140. ContextInformation IConfigurationContextProviderInternal.GetOriginalEvaluationContext()
  141. {
  142. return this.contextHelper.GetOriginalContext(this);
  143. }
  144. }
  145. }