PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 97 lines | 48 code | 11 blank | 38 comment | 8 complexity | bd39b7e2822166c16e980b54a693cce5 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. /// Defines the contract that associates incoming messages with a local operation
  14. /// to customize service execution behavior using
  15. /// <see cref="HttpBinding">HttpBinding</see>.
  16. /// </summary>
  17. public abstract class HttpOperationSelector : IDispatchOperationSelector
  18. {
  19. /// <summary>
  20. /// The name of the message property that will store the name of the selected operation.
  21. /// </summary>
  22. public const string SelectedOperationPropertyName = "HttpOperationName";
  23. /// <summary>
  24. /// Associates a local operation with the incoming method.
  25. /// </summary>
  26. /// <param name="message">The incoming <see cref="HttpRequestMessage"/>.</param>
  27. /// <returns>The name of the operation to be associated with the message.</returns>
  28. public string SelectOperation(HttpRequestMessage message)
  29. {
  30. if (message == null)
  31. {
  32. throw Fx.Exception.ArgumentNull("message");
  33. }
  34. return this.OnSelectOperation(message);
  35. }
  36. /// <summary>
  37. /// Associates a local operation with the incoming method.
  38. /// </summary>
  39. /// <param name="message">The incoming <see cref="Message"/> to be associated with an operation.</param>
  40. /// <returns>The name of the operation to be associated with the message.</returns>
  41. string IDispatchOperationSelector.SelectOperation(ref Message message)
  42. {
  43. if (message == null)
  44. {
  45. throw Fx.Exception.ArgumentNull("message");
  46. }
  47. HttpRequestMessage requestMessage = message.ToHttpRequestMessage();
  48. if (requestMessage == null)
  49. {
  50. throw Fx.Exception.AsError(
  51. new InvalidOperationException(
  52. Http.SR.HttpOperationSelectorNullRequest(
  53. this.GetType().Name,
  54. typeof(HttpRequestMessage).Name,
  55. "SelectOperation")));
  56. }
  57. string operation = this.SelectOperation(requestMessage);
  58. if (operation == null)
  59. {
  60. throw Fx.Exception.AsError(
  61. new InvalidOperationException(
  62. Http.SR.HttpOperationSelectorNullOperation(this.GetType().Name)));
  63. }
  64. message.Properties[SelectedOperationPropertyName] = operation;
  65. ////TODO, CSDMain 205175, need to decide what to do with the tracing
  66. ////if (operation != null)
  67. ////{
  68. //// if (TD2.WebRequestMatchesOperationIsEnabled())
  69. //// {
  70. //// TD2.WebRequestMatchesOperation(
  71. //// requestMessage.RequestUri != null ? requestMessage.RequestUri.ToString() : string.Empty,
  72. //// operation,
  73. //// typeof(HttpOperationSelector).Name);
  74. //// }
  75. ////}
  76. return operation;
  77. }
  78. /// <summary>
  79. /// Abstract method called by <see cref="SelectOperation(HttpRequestMessage)"/>
  80. /// to determine the associated operation. Derived classes must implement this.
  81. /// </summary>
  82. /// <param name="request">The incoming <see cref="HttpRequestMessage"/>.</param>
  83. /// <returns>The name of the operation to be associated with the message.</returns>
  84. protected abstract string OnSelectOperation(HttpRequestMessage request);
  85. }
  86. }