WCF限流操作实际设置方式揭秘

开发 开发工具
WCF限流主要指着就是减轻服务负荷,提高资源利用率,帮助开发人员减轻一定负担。那么,接下来就让我们一起来看看它的设置方法吧。

WCF中有一种操作可以帮助我们减轻程序开发中产生的大负荷问题,以此提高资源的利用率。那么这一方法就是WCF限流。我们就那天将会通过这里介绍的内容详细介绍一下WCF限流的实际设置方法。#t#

WCF限流“允许开发者限制客户端连接数以及服务的负荷。限流可以避免服务的***化,以及分配与使用重要资源的***化。引入限流技术后,一旦超出配置的设置值,WCF就会自动地将等待处理的调用者放入到队列中,然后依次从队列中取出。在队列中等待处理调用时,如果客户端的调用超时,客户端就会获得一个TimeoutException异常。每个服务类型都可以应用限流技术,也就是说,它会影响到服务的所有实例以及服务类型的所有终结点。实现方式是为限流与服务使用的每个通道分发器建立关联。”

WCF限流由ServiceThrottlingBehavior类定义,包括三个重要的属性:MaxConcurrentCalls、MaxConcurrentSessions、MaxConcurrentInstances,它们分别的默认值为16,10和Int.MaxValue。

在翻译过程中,我在查阅MSDN时,发现MaxConcurrentSessions的默认值为64,这让我感觉很奇怪,莫非作者在这里出现了错误。然而经过我仔细地查阅相关资料,发现在WCF的早期版本中,MaxConcurrentSessions的默认值确实为64,但在2006年6月的CTP版本中已经被修改为16。

设置WCF限流值可以通过配置文件,也可以通过编码方式。前者例如:

 

  1. < system.serviceModel> < services> 
  2. < service name = "MyService" behaviorConfiguration = 
    "ThrottledBehavior"> ... < /service>   
  3. < /services> < behaviors> < serviceBehaviors> 
  4. < behavior name = "ThrottledBehavior"> < serviceThrottling 
    maxConcurrentCalls = "12" maxConcurrentSessions = 
    "34" maxConcurrentInstances = "56" />   
  5. < /behavior> < /serviceBehaviors> < /behaviors> < /system.serviceModel>  

 

WCF并没有提供关于限流的特性。但实现该特性的方法非常简单,如下所示:

  1. public class ServiceThrottlingAttribute : Attribute, IServiceBehavior   
  2. {   
  3. private ServiceThrottlingBehavior throttle;   
  4. public ServiceThrottlingAttribute( int maxConcurrentCalls, 
    int maxConcurrentInstances, int maxConcurrentSessions)   
  5. {   
  6. this.throttle = new ServiceThrottlingBehavior();   
  7. throttle.MaxConcurrentCalls = maxConcurrentCalls;   
  8. throttle.MaxConcurrentInstances = maxConcurrentInstances;   
  9. throttle.MaxConcurrentSessions = maxConcurrentSessions; }   
  10. #region IServiceBehavior Members   
  11. void IServiceBehavior.AddBindingParameters(ServiceDescription 
    serviceDescription, ServiceHostBase serviceHostBase, System.Collections.
    ObjectModel.Collection
    < ServiceEndpoint> endpoints, System.
    ServiceModel.Channels.BindingParameterCollection bindingParameters) { }   
  12. void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription 
    serviceDescription, ServiceHostBase serviceHostBase) {   
  13. ServiceThrottlingBehavior currentThrottle = serviceDescription.
    Behaviors.Find
    < ServiceThrottlingBehavior>();   
  14. if (currentThrottle == null) { serviceDescription.Behaviors.Add(this.throttle);   
  15. } }   
  16. void IServiceBehavior.Validate(ServiceDescription serviceDescription, 
    ServiceHostBase serviceHostBase) { } #endregion }   

定义的ServiceThrottlingAttribute特性继承了Attribute,并实现了IServiceBehavior接口。在特性内,则使用了ServiceThrottlingBehavior类,以设置WCF限流的相关值。如果要配置服务的限流值,就可以应用该特性,例如:

  1. [ServiceThrottling(12, 34, 56)]   
  2. class MyService : IMyContract,IDisposable {   
  3. public void MyMethod( ) {   
  4. ChannelDispatcher dispatcher = OperationContext.
    Current.Host.ChannelDispatchers[0] as ChannelDispatcher;   
  5. ServiceThrottle serviceThrottle = dispatcher.ServiceThrottle;   
  6. Trace.WriteLine("MaxConcurrentCalls = " + serviceThrottle.
    MaxConcurrentCalls);   
  7. Trace.WriteLine("MaxSessions = " + serviceThrottle.
    MaxConcurrentSessions);   
  8. Trace.WriteLine("MaxInstances = " + serviceThrottle.
    MaxConcurrentInstances);   
  9. } }  

则WCF限流的输出结果为:

MaxConcurrentCalls = 12

MaxSessions = 56

MaxInstances = 34

责任编辑:曹凯 来源: IT168
相关推荐

2009-12-22 15:02:40

WCF限流

2010-02-26 17:44:51

WCF安全参数

2021-04-21 09:55:24

Redis应用限流

2009-11-09 13:04:53

WCF事物处理

2010-03-01 13:06:49

WCF继承

2010-03-02 10:41:03

IIS托管WCF服务

2010-03-02 17:35:20

WCF服务加载

2010-02-26 10:56:06

WCF Stream

2010-05-12 13:45:25

Mysql 复制设置

2010-02-26 14:05:57

WCF通信方式

2009-12-21 14:49:27

2009-11-06 12:29:23

2009-11-06 13:23:27

WCF模式

2009-11-06 14:40:34

WCF REST架构

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati

2010-02-22 13:28:05

WCF异步调用

2010-02-22 14:18:34

WCF服务验证

2011-12-26 16:33:02

WCF

2009-12-22 15:14:33

WCF调用
点赞
收藏

51CTO技术栈公众号