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限流值可以通過配置文件,也可以通過編碼方式。前者例如:
- < system.serviceModel> < services>
- < service name = "MyService" behaviorConfiguration =
"ThrottledBehavior"> ... < /service>- < /services> < behaviors> < serviceBehaviors>
- < behavior name = "ThrottledBehavior"> < serviceThrottling
maxConcurrentCalls = "12" maxConcurrentSessions =
"34" maxConcurrentInstances = "56" />- < /behavior> < /serviceBehaviors> < /behaviors> < /system.serviceModel>
WCF并沒有提供關于限流的特性。但實現該特性的方法非常簡單,如下所示:
- public class ServiceThrottlingAttribute : Attribute, IServiceBehavior
- {
- private ServiceThrottlingBehavior throttle;
- public ServiceThrottlingAttribute( int maxConcurrentCalls,
int maxConcurrentInstances, int maxConcurrentSessions)- {
- this.throttle = new ServiceThrottlingBehavior();
- throttle.MaxConcurrentCalls = maxConcurrentCalls;
- throttle.MaxConcurrentInstances = maxConcurrentInstances;
- throttle.MaxConcurrentSessions = maxConcurrentSessions; }
- #region IServiceBehavior Members
- void IServiceBehavior.AddBindingParameters(ServiceDescription
serviceDescription, ServiceHostBase serviceHostBase, System.Collections.
ObjectModel.Collection< ServiceEndpoint> endpoints, System.
ServiceModel.Channels.BindingParameterCollection bindingParameters) { }- void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription
serviceDescription, ServiceHostBase serviceHostBase) {- ServiceThrottlingBehavior currentThrottle = serviceDescription.
Behaviors.Find< ServiceThrottlingBehavior>();- if (currentThrottle == null) { serviceDescription.Behaviors.Add(this.throttle);
- } }
- void IServiceBehavior.Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase) { } #endregion }
定義的ServiceThrottlingAttribute特性繼承了Attribute,并實現了IServiceBehavior接口。在特性內,則使用了ServiceThrottlingBehavior類,以設置WCF限流的相關值。如果要配置服務的限流值,就可以應用該特性,例如:
- [ServiceThrottling(12, 34, 56)]
- class MyService : IMyContract,IDisposable {
- public void MyMethod( ) {
- ChannelDispatcher dispatcher = OperationContext.
Current.Host.ChannelDispatchers[0] as ChannelDispatcher;- ServiceThrottle serviceThrottle = dispatcher.ServiceThrottle;
- Trace.WriteLine("MaxConcurrentCalls = " + serviceThrottle.
MaxConcurrentCalls);- Trace.WriteLine("MaxSessions = " + serviceThrottle.
MaxConcurrentSessions);- Trace.WriteLine("MaxInstances = " + serviceThrottle.
MaxConcurrentInstances);- } }
則WCF限流的輸出結果為:
MaxConcurrentCalls = 12
MaxSessions = 56
MaxInstances = 34