Throttling-每秒最大的方法调用数

本文关键字:方法 调用 Throttling- | 更新日期: 2023-09-27 18:20:52

我正在通过WCF使用web服务,我想将服务方法调用限制为每秒N次。有没有一门课能帮助我做到这一点。或者我需要手动更新计数并每秒重置一次。

Throttling-每秒最大的方法调用数

这是一篇关于限制的有用文章

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>