PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 139 lines | 76 code | 15 blank | 48 comment | 12 complexity | bf0a644402ea4bdfbe1438f83b662f40 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.Net.Http;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Dispatcher;
  11. using Microsoft.ApplicationServer.Http.Channels;
  12. using Microsoft.Server.Common;
  13. /// <summary>
  14. /// Defines the methods that enable custom inspection or modification of inbound and
  15. /// outbound application messages in service applications based on
  16. /// <see cref="HttpBinding">HttpBinding</see>.
  17. /// </summary>
  18. public abstract class HttpMessageInspector : IDispatchMessageInspector
  19. {
  20. /// <summary>
  21. /// Called after an inbound message has been received but before the message
  22. /// is dispatched to the intended operation.
  23. /// </summary>
  24. /// <param name="request">The request message.</param>
  25. /// <param name="channel">The incoming channel.</param>
  26. /// <param name="instanceContext">The current service instance.</param>
  27. /// <returns>The object used to correlate state. This object is passed back
  28. /// in the <see cref="IDispatchMessageInspector.BeforeSendReply(ref Message, object)"/> method.</returns>
  29. object IDispatchMessageInspector.AfterReceiveRequest(
  30. ref Message request,
  31. IClientChannel channel,
  32. InstanceContext instanceContext)
  33. {
  34. if (request == null)
  35. {
  36. throw Fx.Exception.ArgumentNull("request");
  37. }
  38. HttpRequestMessage httpRequest = request.ToHttpRequestMessage();
  39. if (httpRequest == null)
  40. {
  41. throw Fx.Exception.AsError(
  42. new InvalidOperationException(
  43. Http.SR.HttpMessageInspectorNullMessage(
  44. this.GetType().Name,
  45. typeof(HttpRequestMessage).Name,
  46. "AfterReceiveRequest")));
  47. }
  48. return this.AfterReceiveRequest(httpRequest);
  49. }
  50. /// <summary>
  51. /// Called after the operation has returned but before the reply message is sent.
  52. /// </summary>
  53. /// <param name="reply">The reply message. This value is null if the operation is one way.</param>
  54. /// <param name="correlationState">The correlation object returned from the <see cref="IDispatchMessageInspector.AfterReceiveRequest(ref Message, IClientChannel, InstanceContext)"/> method.</param>
  55. void IDispatchMessageInspector.BeforeSendReply(
  56. ref Message reply,
  57. object correlationState)
  58. {
  59. if (reply == null)
  60. {
  61. throw Fx.Exception.ArgumentNull("reply");
  62. }
  63. HttpResponseMessage httpResponse = reply.ToHttpResponseMessage();
  64. if (httpResponse == null)
  65. {
  66. throw Fx.Exception.AsError(
  67. new InvalidOperationException(
  68. Http.SR.HttpMessageInspectorNullMessage(
  69. this.GetType().Name,
  70. typeof(HttpResponseMessage).Name,
  71. "BeforeSendReply")));
  72. }
  73. this.BeforeSendReply(httpResponse, correlationState);
  74. }
  75. /// <summary>
  76. /// Called after an inbound message has been received but
  77. /// before the message is dispatched to the intended operation.
  78. /// </summary>
  79. /// <param name="request">The request message</param>
  80. /// <returns>The object used to correlate state. This object is
  81. /// passed back in the <see cref="BeforeSendReply"/>method.</returns>
  82. public object AfterReceiveRequest(HttpRequestMessage request)
  83. {
  84. if (request == null)
  85. {
  86. throw Fx.Exception.ArgumentNull("request");
  87. }
  88. return this.OnAfterReceiveRequest(request);
  89. }
  90. /// <summary>
  91. /// Called after the operation has returned but before the response message is sent.
  92. /// </summary>
  93. /// <param name="response">The response message that will be sent.</param>
  94. /// <param name="correlationState">The correlction object returned from the
  95. /// <see cref="AfterReceiveRequest"/> method.</param>
  96. public void BeforeSendReply(
  97. HttpResponseMessage response,
  98. object correlationState)
  99. {
  100. if (response == null)
  101. {
  102. throw Fx.Exception.ArgumentNull("response");
  103. }
  104. this.OnBeforeSendReply(response, correlationState);
  105. }
  106. /// <summary>
  107. /// Called after an inbound message has been received but
  108. /// before the message is dispatched to the intended operation.
  109. /// </summary>
  110. /// <param name="request">The request message</param>
  111. /// <returns>The object used to correlate state. This object is
  112. /// passed back in the <see cref="BeforeSendReply"/>method.</returns>
  113. protected abstract object OnAfterReceiveRequest(HttpRequestMessage request);
  114. /// <summary>
  115. /// Called after the operation has returned but before the response message is sent.
  116. /// </summary>
  117. /// <param name="response">The response message that will be sent.</param>
  118. /// <param name="correlationState">The correlction object returned from the
  119. /// <see cref="AfterReceiveRequest"/> method.</param>
  120. protected abstract void OnBeforeSendReply(
  121. HttpResponseMessage response,
  122. object correlationState);
  123. }
  124. }