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