2012-01-19 60 views
2

我正在通過WCF使用Web服務,並且我想每秒將服務方法調用限制爲N次。有沒有一個班會幫助我達到這個目標?或者我需要手動更新計數並每秒重置一次。節流 - 每秒最大方法調用

+0

您想要在客戶端,服務器還是兩者都進行調節?使用WCF Web服務的大部分開銷是在序列化和撥打電話。只是要注意,如果你這樣做,以提高性能。 – Yuck

+0

您是在編寫使用Web服務的WCF服務,還是僅通過WCF訪問Web服務?如果您正在使用該服務,則可以通過在WCF服務上設置限制設置來有效地限制對Web服務的調用。如果你只是在使用網絡服務,我認爲你可能會陷入寫自己的反制機制。 –

+0

你想用這種油門來防止什麼? - 你是否在消耗一些具有定時油門或充電模型的資源? –

回答

1

這是節流

http://www.danrigsby.com/blog/index.php/2008/02/20/how-to-throttle-a-wcf-service-help-prevent-dos-attacks-and-maintain-wcf-scalability/

您可以使用內置的節流方法代碼的實用文章:

ServiceHost host = new ServiceHost(
typeof(MyContract), 
new Uri("http://localhost:8080/MyContract")); 
host.AddServiceEndpoint("IMyContract", new WSHttpBinding(), ""); 
System.ServiceModel.Description.ServiceThrottlingBehavior throttlingBehavior = 
new System.ServiceModel.Description.ServiceThrottlingBehavior(); 
throttlingBehavior.MaxConcurrentCalls = 16; 
throttlingBehavior.MaxConcurrentInstances = Int32.MaxValue; 
throttlingBehavior.MaxConcurrentSessions = 10; 
host.Description.Behaviors.Add(throttlingBehavior); 
host.Open(); 

或把它們放進web.config中:

<system.serviceModel> 
<services> 
    <service 
     name="IMyContract" 
     behaviorConfiguration="myContract"> 
     <host> 
      <baseAddresses> 
       <add baseAddress="http://localhost:8080/MyContract"/> 
      </baseAddresses> 
     </host> 
     <endpoint 
      name="wsHttp" 
      address="" 
      binding="wsHttpBinding" 
      contract="IMyContract"> 
     </endpoint> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
     <behavior name="myContract"> 
      <serviceMetadata httpGetEnabled="True" /> 
      <serviceThrottling 
       maxConcurrentCalls="16" 
       maxConcurrentInstances="2147483647" 
       maxConcurrentSessions="10"/> 
     </behavior> 
    </serviceBehaviors> 
</behaviors>