WCF 项目应用连载[8] - 绑定、服务、行为 大数据传输与限流 - 下 (ServiceThrottlingAttribute)


因为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
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值