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