andrew4582

WCF Service Throttle Behavior Extension Element

May 28th, 2013
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.ServiceModel.Description;
  4. using System.ServiceModel.Dispatcher;
  5. using System.Threading;
  6.  
  7. namespace Common.Diagnostics
  8. {
  9.     /// <summary>
  10.     /// Behavior extension to throttle WCF client requests. See <see cref="ServiceThrottleBehaviorExtension"/> to configure
  11.     /// </summary>
  12.     public class ServiceThrottleBehavior : IClientMessageInspector, IEndpointBehavior
  13.     {
  14.         public int SleepMilliseconds { get; set; }
  15.  
  16.         public ServiceThrottleBehavior(int sleepMS)
  17.         {
  18.             this.SleepMilliseconds = sleepMS;
  19.         }
  20.  
  21.         public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
  22.         {
  23.             string requestAction = ParseAction(request.Headers.Action);
  24.             if (this.SleepMilliseconds > 0)
  25.             {
  26.                 System.Diagnostics.Debug.WriteLine("{0} sleeping {1:N0} ms", requestAction, this.SleepMilliseconds);
  27.                 Thread.Sleep(this.SleepMilliseconds);
  28.             }
  29.             return null;
  30.         }
  31.  
  32.         public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  33.         {
  34.  
  35.         }
  36.         /// <summary>
  37.         /// Gets the WCF Server name and method from the reply action header
  38.         /// </summary>
  39.         string ParseAction(string actionHeader)
  40.         {
  41.             string actionMethod = actionHeader;
  42.             Uri actionUri = null;
  43.             if (Uri.TryCreate(actionHeader, UriKind.Absolute, out actionUri))
  44.             {
  45.                 actionMethod = string.Join(".", actionUri.Segments
  46.                                                         .Where(t => !t.Equals(actionUri.Scheme))
  47.                                                         .Where(t => !t.Equals(actionUri.Host))
  48.                                                         .Where(t => !t.Equals("/"))
  49.                                                         .Select(t => t.Trim('/')).ToArray());
  50.  
  51.                 if (string.IsNullOrEmpty(actionMethod))
  52.                     actionMethod = actionHeader;
  53.             }
  54.             return actionMethod;
  55.         }
  56.  
  57.         public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  58.         {
  59.             // Add this implementation to the inspectors.
  60.             clientRuntime.MessageInspectors.Add(this);
  61.         }
  62.  
  63.         #region Not implemented IEndpointBehavior methods
  64.         public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
  65.  
  66.         public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
  67.  
  68.         public void Validate(ServiceEndpoint endpoint) { }
  69.         #endregion
  70.  
  71.     }
  72.  
  73.     /// <summary>
  74.     /// Extension element used to throttle service requests, used in app.config
  75.     /// </summary>
  76.     /// <example>
  77.     /// <![CDATA[
  78.     /// <extensions>
  79.     ///     <behaviorExtensions>
  80.     ///         <add name="serviceThrottleBehavior" type="Common.Diagnostics.ServiceThrottleBehaviorExtension, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  81.     ///     </behaviorExtensions>
  82.     /// </extensions>
  83.     /// /*
  84.     /// <behaviors>
  85.     ///     <endpointBehaviors>
  86.     ///         <behavior name="YourServiceBehavior">
  87.     ///             <serviceThrottleBehavior sleepMilliseconds="500"/>
  88.     ///         </behavior>
  89.     ///     </endpointBehaviors>
  90.     /// </behaviors>
  91.     /// */
  92.     ///  ]]>
  93.     /// </example>
  94.     public class ServiceThrottleBehaviorExtension : BehaviorExtensionElement
  95.     {
  96.         protected override object CreateBehavior()
  97.         {
  98.             return new ServiceThrottleBehavior(this.SleepMilliseconds);
  99.         }
  100.  
  101.         public override Type BehaviorType
  102.         {
  103.             get
  104.             {
  105.                 return typeof(ServiceThrottleBehavior);
  106.             }
  107.         }
  108.  
  109.         [ConfigurationProperty("sleepMilliseconds", DefaultValue = 0, IsRequired = false)]
  110.         public int SleepMilliseconds
  111.         {
  112.             get
  113.             {
  114.                 var raw = base["sleepMilliseconds"];
  115.                 if (raw == null)
  116.                     return 0;
  117.                 else
  118.                 {
  119.                     int val = 0;
  120.                     if (int.TryParse(raw.ToString(), out val))
  121.                         return val;
  122.                 }
  123.                 return 0;
  124.             }
  125.             set { base["sleepMilliseconds"] = value; }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment