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