PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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