PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Http/Configuration/HttpMemoryEndpointElement.cs

#
C# | 229 lines | 146 code | 24 blank | 59 comment | 11 complexity | 637995181b9edbe46b345cc7abbb5e3e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace Microsoft.ApplicationServer.Http.Configuration
  5. {
  6. using System;
  7. using System.Configuration;
  8. using System.Net.Http;
  9. using System.ServiceModel.Configuration;
  10. using System.ServiceModel.Description;
  11. using Microsoft.ApplicationServer.Http.Description;
  12. using Microsoft.Server.Common;
  13. using Microsoft.ServiceModel.Configuration;
  14. /// <summary>
  15. /// Class that provides an endpoint element for the <see cref="HttpMemoryBinding"/> binding.
  16. /// </summary>
  17. public sealed class HttpMemoryEndpointElement : StandardEndpointElement
  18. {
  19. private const string HttpBindingCollectionElementName = "httpMemoryBinding";
  20. private const string NameAsKeyPropertyName = "name";
  21. private static readonly Type httpClientType = typeof(HttpClient);
  22. private static readonly Type httpMemoryEndpointType = typeof(HttpMemoryEndpoint);
  23. private ConfigurationPropertyCollection properties;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="HttpMemoryEndpoint"/> class.
  26. /// </summary>
  27. public HttpMemoryEndpointElement()
  28. {
  29. }
  30. /// <summary>
  31. /// Gets or sets a value indicating whether the automatic help page will be available.
  32. /// </summary>
  33. [ConfigurationProperty(HttpConfigurationStrings.HelpEnabled, DefaultValue = HttpBehavior.DefaultHelpEnabled)]
  34. public bool HelpEnabled
  35. {
  36. get { return (bool)base[HttpConfigurationStrings.HelpEnabled]; }
  37. set { base[HttpConfigurationStrings.HelpEnabled] = value; }
  38. }
  39. /// <summary>
  40. /// Gets or sets a value indicating whether the web-based test client will be available.
  41. /// </summary>
  42. [ConfigurationProperty(HttpConfigurationStrings.TestClientEnabled, DefaultValue = HttpBehavior.DefaultTestClientEnabled)]
  43. public bool TestClientEnabled
  44. {
  45. get { return (bool)base[HttpConfigurationStrings.TestClientEnabled]; }
  46. set { base[HttpConfigurationStrings.TestClientEnabled] = value; }
  47. }
  48. /// <summary>
  49. /// Gets or sets a value specifying how trailing slashes in the <see cref="Uri"/> will be handled.
  50. /// </summary>
  51. [ConfigurationProperty(HttpConfigurationStrings.TrailingSlashMode, DefaultValue = HttpBehavior.DefaultTrailingSlashMode)]
  52. [ServiceModelEnumValidator(typeof(TrailingSlashModeHelper))]
  53. public TrailingSlashMode TrailingSlashMode
  54. {
  55. get { return (TrailingSlashMode)base[HttpConfigurationStrings.TrailingSlashMode]; }
  56. set { base[HttpConfigurationStrings.TrailingSlashMode] = value; }
  57. }
  58. /// <summary>
  59. /// Gets or sets the <see cref="OperationHandlerFactory"/>.
  60. /// </summary>
  61. [ConfigurationProperty(HttpConfigurationStrings.OperationHandlerFactory, DefaultValue = "")]
  62. [StringValidator(MinLength = 0)]
  63. public string OperationHandlerFactory
  64. {
  65. get
  66. {
  67. return (string)base[HttpConfigurationStrings.OperationHandlerFactory];
  68. }
  69. set
  70. {
  71. if (String.IsNullOrWhiteSpace(value))
  72. {
  73. value = String.Empty;
  74. }
  75. base[HttpConfigurationStrings.OperationHandlerFactory] = value;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets the type of the endpoint.
  80. /// </summary>
  81. protected override Type EndpointType
  82. {
  83. get
  84. {
  85. return httpMemoryEndpointType;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets the collection of properties for the current endpoint element.
  90. /// </summary>
  91. protected override ConfigurationPropertyCollection Properties
  92. {
  93. get
  94. {
  95. if (this.properties == null)
  96. {
  97. ConfigurationPropertyCollection localProperties = new ConfigurationPropertyCollection();
  98. localProperties.Add(new ConfigurationProperty(NameAsKeyPropertyName, typeof(string), string.Empty, null, new StringValidator(0, 0x7fffffff, null), ConfigurationPropertyOptions.IsKey));
  99. localProperties.Add(new ConfigurationProperty(HttpConfigurationStrings.HelpEnabled, typeof(bool), HttpBehavior.DefaultHelpEnabled, null, null, ConfigurationPropertyOptions.None));
  100. localProperties.Add(new ConfigurationProperty(HttpConfigurationStrings.TestClientEnabled, typeof(bool), HttpBehavior.DefaultTestClientEnabled, null, null, ConfigurationPropertyOptions.None));
  101. localProperties.Add(new ConfigurationProperty(HttpConfigurationStrings.TrailingSlashMode, typeof(TrailingSlashMode), HttpBehavior.DefaultTrailingSlashMode, null, new ServiceModelEnumValidator(typeof(TrailingSlashModeHelper)), ConfigurationPropertyOptions.None));
  102. localProperties.Add(new ConfigurationProperty(HttpConfigurationStrings.OperationHandlerFactory, typeof(string), string.Empty, null, new System.Configuration.StringValidator(0), System.Configuration.ConfigurationPropertyOptions.None));
  103. this.properties = localProperties;
  104. }
  105. return this.properties;
  106. }
  107. }
  108. /// <summary>
  109. /// When implemented in a derived class, creates a service endpoint from message information
  110. /// contained in the specified <see cref="ContractDescription"/>.
  111. /// </summary>
  112. /// <param name="contractDescription">Information about what is contained in messages
  113. /// being sent from an endpoint.</param>
  114. /// <returns>A service endpoint.</returns>
  115. protected override ServiceEndpoint CreateServiceEndpoint(ContractDescription contractDescription)
  116. {
  117. return new HttpMemoryEndpoint(contractDescription);
  118. }
  119. /// <summary>
  120. /// When called from a derived class, initializes and verifies the format of the
  121. /// specified service endpoint element in a service application configuration file.
  122. /// </summary>
  123. /// <param name="channelEndpointElement">An endpoint element that defineds the physical transfer
  124. /// of messages back and forth between the client and the service.</param>
  125. protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
  126. {
  127. throw Fx.Exception.AsError(
  128. new NotSupportedException(
  129. Http.SR.HttpEndpointNotSupported(
  130. httpMemoryEndpointType.Name,
  131. httpClientType.Name)));
  132. }
  133. /// <summary>
  134. /// When called from a derived class, initializes and verifies the format of the
  135. /// specified service endpoint element in a service application configuration file.
  136. /// </summary>
  137. /// <param name="serviceEndpointElement">A service endpoint element that enables clients to find and
  138. /// communicate with a service.</param>
  139. protected override void OnInitializeAndValidate(ServiceEndpointElement serviceEndpointElement)
  140. {
  141. if (string.IsNullOrEmpty(serviceEndpointElement.Binding))
  142. {
  143. serviceEndpointElement.Binding = HttpBindingCollectionElementName;
  144. }
  145. else if (!string.Equals(serviceEndpointElement.Binding, HttpBindingCollectionElementName, StringComparison.Ordinal))
  146. {
  147. throw Fx.Exception.AsError(
  148. new NotSupportedException(
  149. Http.SR.HttpEndpointRequiredBinding(
  150. typeof(HttpMemoryEndpoint).Name,
  151. HttpBindingCollectionElementName)));
  152. }
  153. }
  154. /// <summary>
  155. /// When called from a derived class, loads the service description information
  156. /// from the configuration file and applies it to the runtime being constructed.
  157. /// </summary>
  158. /// <param name="endpoint">And endpoint that enables clients to find and communicate with a service.</param>
  159. /// <param name="serviceEndpointElement">A service endpoint element of a service application.</param>
  160. protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
  161. {
  162. this.InternalOnApplyConfiguration(endpoint);
  163. }
  164. /// <summary>
  165. /// When called from a derived class, loads the service description information
  166. /// from the configuration file and applies it to the runtime being constructed.
  167. /// </summary>
  168. /// <param name="endpoint">And endpoint that enables clients to find and communicate with a service.</param>
  169. /// <param name="channelEndpointElement">An endpoint element that defined the physical
  170. /// transfer of messages ack and forth between the client and the service.</param>
  171. protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement channelEndpointElement)
  172. {
  173. throw Fx.Exception.AsError(
  174. new NotSupportedException(
  175. Http.SR.HttpEndpointNotSupported(
  176. httpMemoryEndpointType.Name,
  177. httpClientType.Name)));
  178. }
  179. private void InternalOnApplyConfiguration(ServiceEndpoint endpoint)
  180. {
  181. HttpMemoryEndpoint httpMemoryEndpoint = endpoint as HttpMemoryEndpoint;
  182. Fx.Assert(httpMemoryEndpoint != null, "The endpoint should be of type HttpMemoryEndpoint since this is what was returned with CreateServiceEndpoint().");
  183. if (this.IsSet(HttpConfigurationStrings.HelpEnabled))
  184. {
  185. httpMemoryEndpoint.HelpEnabled = this.HelpEnabled;
  186. }
  187. if (this.IsSet(HttpConfigurationStrings.TestClientEnabled))
  188. {
  189. httpMemoryEndpoint.TestClientEnabled = this.TestClientEnabled;
  190. }
  191. if (this.IsSet(HttpConfigurationStrings.TrailingSlashMode))
  192. {
  193. httpMemoryEndpoint.TrailingSlashMode = this.TrailingSlashMode;
  194. }
  195. if (this.IsSet(HttpConfigurationStrings.OperationHandlerFactory))
  196. {
  197. httpMemoryEndpoint.OperationHandlerFactory = HttpBehaviorElement.GetHttpOperationHandlerFactory(this.OperationHandlerFactory);
  198. }
  199. }
  200. private bool IsSet(string propertyName)
  201. {
  202. return this.ElementInformation.Properties[propertyName].IsModified;
  203. }
  204. }
  205. }