PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 129 lines | 71 code | 14 blank | 44 comment | 15 complexity | 33f1cb450a6ecfa58502a0d4e1dedc88 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.Channels;
 10    using System.ServiceModel.Dispatcher;
 11    using Microsoft.ApplicationServer.Http.Channels;
 12    using Microsoft.Server.Common;
 13
 14    /// <summary>
 15    /// Abstract base class that defines methods to deserialize request messages
 16    /// and serialize response messages for the
 17    /// <see cref="HttpBinding">HttpBinding</see>.
 18    /// </summary>
 19    public abstract class HttpMessageFormatter : IDispatchMessageFormatter
 20    {
 21        /// <summary>
 22        /// Deserializes a message into an array of parameters.
 23        /// </summary>
 24        /// <param name="message">The incoming message.</param>
 25        /// <param name="parameters">The objects that are passed to the operation as parameters.</param>
 26        void IDispatchMessageFormatter.DeserializeRequest(Message message, object[] parameters)
 27        {
 28            if (message == null)
 29            {
 30                throw Fx.Exception.ArgumentNull("message");
 31            }
 32
 33            HttpRequestMessage request = message.ToHttpRequestMessage();
 34            if (request == null)
 35            {
 36                throw Fx.Exception.AsError(
 37                    new InvalidOperationException(
 38                        Http.SR.HttpMessageFormatterNullMessage(this.GetType().Name, typeof(HttpRequestMessage).Name, "DeserializeRequest")));
 39            }
 40
 41            this.DeserializeRequest(request, parameters);
 42        }
 43
 44        /// <summary>
 45        /// Serializes a reply message from a specified message version, array of parameters, and a return value.
 46        /// </summary>
 47        /// <param name="messageVersion">The SOAP message version.</param>
 48        /// <param name="parameters">The out parameters.</param>
 49        /// <param name="result">The return value.</param>
 50        /// <returns>The serialized reply message.</returns>
 51        Message IDispatchMessageFormatter.SerializeReply(
 52                                              MessageVersion messageVersion, 
 53                                              object[] parameters, 
 54                                              object result)
 55        {
 56            if (messageVersion != MessageVersion.None)
 57            {
 58                throw Fx.Exception.AsError(
 59                    new NotSupportedException(
 60                        Http.SR.HttpMessageFormatterMessageVersion(this.GetType(), typeof(MessageVersion), "None")));
 61            }
 62
 63            HttpResponseMessage response = this.SerializeReply(parameters, result);
 64            Fx.Assert(response != null, "Response cannot be null.");
 65            return response.ToMessage();
 66        }
 67
 68        /// <summary>
 69        /// Deserializes a message into an array of parameters.
 70        /// </summary>
 71        /// <param name="request">The incoming message.</param>
 72        /// <param name="parameters">The objects that are passed to the operation as parameters.</param>
 73        public void DeserializeRequest(HttpRequestMessage request, object[] parameters)
 74        {
 75            if (request == null)
 76            {
 77                throw Fx.Exception.ArgumentNull("request");
 78            }
 79
 80            if (parameters == null)
 81            {
 82                throw Fx.Exception.ArgumentNull("parameters");
 83            }
 84
 85            this.OnDeserializeRequest(request, parameters);
 86        }
 87
 88        /// <summary>
 89        /// Serializes a reply message from an array of parameters and a return value into the
 90        /// given <paramref name="result"/>.
 91        /// </summary>
 92        /// <param name="parameters">The out parameters.</param>
 93        /// <param name="result">The return value.</param>
 94        /// <returns>The <see cref="HttpResponseMessage"/> to return.  It cannot be <c>null</c>.</returns>
 95        public HttpResponseMessage SerializeReply(object[] parameters, object result)
 96        {
 97            if (parameters == null)
 98            {
 99                throw Fx.Exception.ArgumentNull("parameters");
100            }
101
102            HttpResponseMessage responseMessage = this.OnSerializeReply(parameters, result);
103            if (responseMessage == null)
104            {
105                throw Fx.Exception.AsError(
106                    new InvalidOperationException(
107                        Http.SR.HttpMessageFormatterNullMessage(this.GetType(), typeof(HttpResponseMessage).Name, "SerializeReply")));
108            }
109
110            return responseMessage;
111        }
112
113        /// <summary>
114        /// Deserializes a message into an array of parameters.
115        /// </summary>
116        /// <param name="message">The incoming message.</param>
117        /// <param name="parameters">The objects that are passed to the operation as parameters.</param>
118        protected abstract void OnDeserializeRequest(HttpRequestMessage message, object[] parameters);
119
120        /// <summary>
121        /// Serializes a reply message from an array of parameters and a return value into the
122        /// given <paramref name="result"/>.
123        /// </summary>
124        /// <param name="parameters">The out parameters.</param>
125        /// <param name="result">The return value.</param>
126        /// <returns>The <see cref="HttpResponseMessage"/> to return.  It cannot be <c>null</c>.</returns>
127        protected abstract HttpResponseMessage OnSerializeReply(object[] parameters, object result);
128    }
129}