自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF限流操作實際設(shè)置方式揭秘

開發(fā) 開發(fā)工具
WCF限流主要指著就是減輕服務(wù)負(fù)荷,提高資源利用率,幫助開發(fā)人員減輕一定負(fù)擔(dān)。那么,接下來就讓我們一起來看看它的設(shè)置方法吧。

WCF中有一種操作可以幫助我們減輕程序開發(fā)中產(chǎn)生的大負(fù)荷問題,以此提高資源的利用率。那么這一方法就是WCF限流。我們就那天將會通過這里介紹的內(nèi)容詳細(xì)介紹一下WCF限流的實際設(shè)置方法。#t#

WCF限流“允許開發(fā)者限制客戶端連接數(shù)以及服務(wù)的負(fù)荷。限流可以避免服務(wù)的***化,以及分配與使用重要資源的***化。引入限流技術(shù)后,一旦超出配置的設(shè)置值,WCF就會自動地將等待處理的調(diào)用者放入到隊列中,然后依次從隊列中取出。在隊列中等待處理調(diào)用時,如果客戶端的調(diào)用超時,客戶端就會獲得一個TimeoutException異常。每個服務(wù)類型都可以應(yīng)用限流技術(shù),也就是說,它會影響到服務(wù)的所有實例以及服務(wù)類型的所有終結(jié)點。實現(xiàn)方式是為限流與服務(wù)使用的每個通道分發(fā)器建立關(guān)聯(lián)。”

WCF限流由ServiceThrottlingBehavior類定義,包括三個重要的屬性:MaxConcurrentCalls、MaxConcurrentSessions、MaxConcurrentInstances,它們分別的默認(rèn)值為16,10和Int.MaxValue。

在翻譯過程中,我在查閱MSDN時,發(fā)現(xiàn)MaxConcurrentSessions的默認(rèn)值為64,這讓我感覺很奇怪,莫非作者在這里出現(xiàn)了錯誤。然而經(jīng)過我仔細(xì)地查閱相關(guān)資料,發(fā)現(xiàn)在WCF的早期版本中,MaxConcurrentSessions的默認(rèn)值確實為64,但在2006年6月的CTP版本中已經(jīng)被修改為16。

設(shè)置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并沒有提供關(guān)于限流的特性。但實現(xiàn)該特性的方法非常簡單,如下所示:

  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,并實現(xiàn)了IServiceBehavior接口。在特性內(nèi),則使用了ServiceThrottlingBehavior類,以設(shè)置WCF限流的相關(guān)值。如果要配置服務(wù)的限流值,就可以應(yīng)用該特性,例如:

  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限流的輸出結(jié)果為:

MaxConcurrentCalls = 12

MaxSessions = 56

MaxInstances = 34

責(zé)任編輯:曹凱 來源: IT168
相關(guān)推薦

2009-12-22 15:02:40

WCF限流

2010-02-26 17:44:51

WCF安全參數(shù)

2021-04-21 09:55:24

Redis應(yīng)用限流

2009-11-09 13:04:53

WCF事物處理

2010-03-01 13:06:49

WCF繼承

2010-03-02 10:41:03

IIS托管WCF服務(wù)

2010-02-26 14:05:57

WCF通信方式

2010-02-26 10:56:06

WCF Stream

2010-03-02 17:35:20

WCF服務(wù)加載

2009-11-06 14:40:34

WCF REST架構(gòu)

2009-12-21 14:49:27

2010-05-12 13:45:25

Mysql 復(fù)制設(shè)置

2009-11-06 13:23:27

WCF模式

2009-11-06 12:29:23

2011-12-26 16:33:02

WCF

2010-02-22 14:18:34

WCF服務(wù)驗證

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati

2009-12-22 15:14:33

WCF調(diào)用

2009-11-06 15:47:17

WCF Windows
點贊
收藏

51CTO技術(shù)棧公眾號