因为ORM的原因,对Attribute编程有一种情节。。所以这节的出现,完全是因为在WCF对自定义Attribute的一种应用。
WCF 项目应用连载[7] - 绑定、服务、行为 大数据传输与限流 - 上
前面一节已经讲得差不多够了。
对WCF的限流,这节,提供一个类。ServiceThrottlingAttribute
让你以硬编码方式使用WCF服务限流
[ServiceThrottling(50,200,100)] [ServiceContract(CallbackContract = typeof(ILigAgentCallback))] public interface ILigAgent
public class ServiceThrottlingAttribute : Attribute,IServiceBehavior { #region Constructors public ServiceThrottlingAttribute() : this(defaultCalls, defaultInstances, defaultSession) { } public ServiceThrottlingAttribute(int maxCalls, int maxInstances, int maxSessions) { this.Initialize(maxCalls, maxInstances, maxSessions); } #endregion #region Interfaces /// <summary> /// /// </summary> /// <param name="serviceDescriptiong"></param> /// <param name="serviceHostBase"></param> /// <param name="endpoints"></param> /// <param name="bindingParameters"></param> void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescriptiong, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { } /// <summary> /// /// </summary> /// <param name="serviceDescription"></param> /// <param name="serviceHostBase"></param> void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { ServiceThrottlingBehavior tbehavior = serviceDescription.Behaviors.Find<ServiceThrottlingBehavior>(); if (tbehavior == null) { serviceDescription.Behaviors.Add(this.throttlingBehavior); } } /// <summary> /// /// </summary> /// <param name="serviceDescription"></param> /// <param name="serviceHostBase"></param> void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } #endregion #region Private Helpers private void Initialize(int maxCalls, int maxInstances, int maxSessions) { this.throttlingBehavior = new ServiceThrottlingBehavior(); this.throttlingBehavior.MaxConcurrentCalls = maxCalls; this.throttlingBehavior.MaxConcurrentInstances = maxInstances; this.throttlingBehavior.MaxConcurrentSessions = maxSessions; } #endregion #region Fields private const int defaultThrottling = 50; private const int defaultCalls = 50; private const int defaultInstances = 200; private const int defaultSession = 100; private ServiceThrottlingBehavior throttlingBehavior = null; #endregion }