WCF限流操作實際設(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限流值可以通過配置文件,也可以通過編碼方式。前者例如:
- < 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并沒有提供關(guān)于限流的特性。但實現(xiàn)該特性的方法非常簡單,如下所示:
- 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,并實現(xiàn)了IServiceBehavior接口。在特性內(nèi),則使用了ServiceThrottlingBehavior類,以設(shè)置WCF限流的相關(guān)值。如果要配置服務(wù)的限流值,就可以應(yīng)用該特性,例如:
- [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限流的輸出結(jié)果為:
MaxConcurrentCalls = 12
MaxSessions = 56
MaxInstances = 34