限制 - 每秒最大方法调用次数

时间:2012-01-19 14:18:52

标签: c# wcf throttling

我正在通过WCF使用Web服务,我希望每秒将服务方法调用限制为N.是否有一个课程可以帮助我实现这一目标。或者我是否需要手动更新计数并每秒重置一次。

1 个答案:

答案 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>