PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Http/Dispatcher/HttpResponseException.cs

#
C# | 121 lines | 64 code | 12 blank | 45 comment | 3 complexity | 3183ab9c1a60ac644b55867bf8784fef MD5 | raw file
  1// <copyright>
  2//   Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ApplicationServer.Http.Dispatcher
  6{
  7    using System;
  8    using System.Diagnostics.CodeAnalysis;
  9    using System.Net;
 10    using System.Net.Http;
 11    using System.Runtime.Serialization;
 12    using System.Security.Permissions;
 13    using System.ServiceModel;
 14    using Microsoft.Server.Common;
 15
 16    /// <summary>
 17    /// An exception that allows for a given <see cref="HttpResponseMessage"/> 
 18    /// to be returned to the client.
 19    /// </summary>
 20    [Serializable]
 21    public class HttpResponseException : Exception
 22    {
 23        private const string ResponsePropertyName = "Response";
 24
 25        /// <summary>
 26        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 27        /// </summary>
 28        public HttpResponseException()
 29            : this(HttpStatusCode.InternalServerError)
 30        {
 31        }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 35        /// </summary>
 36        /// <param name="message">The message that describes the error.</param>
 37        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "disposed later.")]
 38        public HttpResponseException(string message)
 39            : base(message)
 40        {
 41            this.InitializeResponse(new HttpResponseMessage(HttpStatusCode.InternalServerError));
 42        }
 43
 44        /// <summary>
 45        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 46        /// </summary>
 47        /// <param name="message">The message that describes the error.</param>
 48        /// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
 49        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "disposed later.")]
 50        public HttpResponseException(string message, Exception innerException)
 51            : base(message, innerException)
 52        {
 53            this.InitializeResponse(new HttpResponseMessage(HttpStatusCode.InternalServerError));
 54        }
 55
 56        /// <summary>
 57        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 58        /// </summary>
 59        /// <param name="statusCode">The status code to use with the <see cref="HttpResponseMessage"/>.</param>
 60        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "disposed later.")]
 61        public HttpResponseException(HttpStatusCode statusCode)
 62            : this(new HttpResponseMessage(statusCode))
 63        {
 64        }
 65
 66        /// <summary>
 67        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 68        /// </summary>
 69        /// <param name="response">The response message.</param>
 70        public HttpResponseException(HttpResponseMessage response)
 71            : base(Http.SR.HttpResponseExceptionMessage(ResponsePropertyName))
 72        {
 73            if (response == null)
 74            {
 75                throw Fx.Exception.ArgumentNull("response");
 76            }
 77
 78            this.InitializeResponse(response);
 79        }
 80
 81        /// <summary>
 82        /// Initializes a new instance of the <see cref="HttpResponseException"/> class.
 83        /// </summary>
 84        /// <param name="serializationInfo">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
 85        /// <param name="streamingContext">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
 86        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "disposed later.")]
 87        protected HttpResponseException(SerializationInfo serializationInfo, StreamingContext streamingContext)
 88            : base(serializationInfo, streamingContext)
 89        {
 90            this.InitializeResponse(new HttpResponseMessage(HttpStatusCode.InternalServerError));
 91        }
 92
 93        /// <summary>
 94        /// Gets the <see cref="HttpResponseMessage"/> to return to the client.
 95        /// </summary>
 96        public HttpResponseMessage Response { get; private set; }
 97
 98        /// <summary>
 99        /// Sets the <see cref="SerializationInfo"/> with information about the exception.
100        /// </summary>
101        /// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
102        /// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
103        /// <exception cref="T:ArgumentNullException">The <paramref name="info"/> parameter is a null reference.</exception>
104        /// <PermissionSet>
105        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
106        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
107        /// </PermissionSet>
108        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
109        public override void GetObjectData(SerializationInfo info, StreamingContext context)
110        {
111            base.GetObjectData(info, context);
112        }
113
114        private void InitializeResponse(HttpResponseMessage response)
115        {
116            Fx.Assert(response != null, "Response cannot be null!");
117            this.Response = response;
118            this.Response.RequestMessage = OperationContext.Current.GetHttpRequestMessage();
119        }
120    }
121}