c# - throttle - 每秒最大方法调用

标签 c# wcf throttling

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

最佳答案

这是一篇关于 throttle 的有用文章

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

您可以在代码中使用内置的 throttle 方法:

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>

关于c# - throttle - 每秒最大方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8927606/

相关文章:

javascript - 如何模拟慢速互联网连接以获得用户体验?

sql - WCF 服务 : ASP. NET 缓存或 SQL

python - 测试 Django throttle

c# - WPF - 无法在 WebBrowser 控件中从本地计算机打开文件

c# - 如何使用具有相同契约(Contract)和绑定(bind)的动态端点创建多个 wcf 服务实例而不保存在 app.config 中?

c# - 如何将经度和纬度字符串转换为 double ?

c# - 如何更早发现不良端点?

Java 客户端使用的本地主机上的 C# WCF Web 服务

c# - 在 C# 中捕获所有异常

c# - 数组访问的安全元素