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