Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.ServiceModel.Description;
- using System.ServiceModel.Dispatcher;
- using System.Threading;
- namespace Common.Diagnostics
- {
- /// <summary>
- /// Behavior extension to throttle WCF client requests. See <see cref="ServiceThrottleBehaviorExtension"/> to configure
- /// </summary>
- public class ServiceThrottleBehavior : IClientMessageInspector, IEndpointBehavior
- {
- public int SleepMilliseconds { get; set; }
- public ServiceThrottleBehavior(int sleepMS)
- {
- this.SleepMilliseconds = sleepMS;
- }
- public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
- {
- string requestAction = ParseAction(request.Headers.Action);
- if (this.SleepMilliseconds > 0)
- {
- System.Diagnostics.Debug.WriteLine("{0} sleeping {1:N0} ms", requestAction, this.SleepMilliseconds);
- Thread.Sleep(this.SleepMilliseconds);
- }
- return null;
- }
- public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
- {
- }
- /// <summary>
- /// Gets the WCF Server name and method from the reply action header
- /// </summary>
- string ParseAction(string actionHeader)
- {
- string actionMethod = actionHeader;
- Uri actionUri = null;
- if (Uri.TryCreate(actionHeader, UriKind.Absolute, out actionUri))
- {
- actionMethod = string.Join(".", actionUri.Segments
- .Where(t => !t.Equals(actionUri.Scheme))
- .Where(t => !t.Equals(actionUri.Host))
- .Where(t => !t.Equals("/"))
- .Select(t => t.Trim('/')).ToArray());
- if (string.IsNullOrEmpty(actionMethod))
- actionMethod = actionHeader;
- }
- return actionMethod;
- }
- public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
- {
- // Add this implementation to the inspectors.
- clientRuntime.MessageInspectors.Add(this);
- }
- #region Not implemented IEndpointBehavior methods
- public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { }
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
- public void Validate(ServiceEndpoint endpoint) { }
- #endregion
- }
- /// <summary>
- /// Extension element used to throttle service requests, used in app.config
- /// </summary>
- /// <example>
- /// <![CDATA[
- /// <extensions>
- /// <behaviorExtensions>
- /// <add name="serviceThrottleBehavior" type="Common.Diagnostics.ServiceThrottleBehaviorExtension, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
- /// </behaviorExtensions>
- /// </extensions>
- /// /*
- /// <behaviors>
- /// <endpointBehaviors>
- /// <behavior name="YourServiceBehavior">
- /// <serviceThrottleBehavior sleepMilliseconds="500"/>
- /// </behavior>
- /// </endpointBehaviors>
- /// </behaviors>
- /// */
- /// ]]>
- /// </example>
- public class ServiceThrottleBehaviorExtension : BehaviorExtensionElement
- {
- protected override object CreateBehavior()
- {
- return new ServiceThrottleBehavior(this.SleepMilliseconds);
- }
- public override Type BehaviorType
- {
- get
- {
- return typeof(ServiceThrottleBehavior);
- }
- }
- [ConfigurationProperty("sleepMilliseconds", DefaultValue = 0, IsRequired = false)]
- public int SleepMilliseconds
- {
- get
- {
- var raw = base["sleepMilliseconds"];
- if (raw == null)
- return 0;
- else
- {
- int val = 0;
- if (int.TryParse(raw.ToString(), out val))
- return val;
- }
- return 0;
- }
- set { base["sleepMilliseconds"] = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment