【问题标题】:WCF: How do I add a ServiceThrottlingBehavior to a WCF Service?WCF:如何将 ServiceThrottlingBehavior 添加到 WCF 服务?
【发布时间】:2009-04-27 16:45:54
【问题描述】:

我有以下代码用于返回我的 WCF 服务实例ServiceClient

    var readerQuotas = new XmlDictionaryReaderQuotas()
    {
        MaxDepth = 6000000,
        MaxStringContentLength = 6000000,
        MaxArrayLength = 6000000,
        MaxBytesPerRead = 6000000,
        MaxNameTableCharCount = 6000000
    };


    var throttlingBehaviour = new ServiceThrottlingBehavior(){MaxConcurrentCalls=500,MaxConcurrentInstances=500,MaxConcurrentSessions = 500}; 
    binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};

    dualBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None)
                      {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};

    endpointAddress = new EndpointAddress("http://localhost:28666/DBInteractionGateway.svc"); 

    return new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), dualBinding, endpointAddress);

最近我遇到了一些超时问题,所以我决定添加一个限制行为,如下所示:

    var throttlingBehaviour = new ServiceThrottlingBehavior () {
        MaxConcurrentCalls=500, 
        MaxConcurrentInstances=500,
        MaxConcurrentSessions = 500
    }; 

我的问题是,我应该在上面的代码中的哪个位置将这个 throttlingBehaviour 添加到我的 MusicRepo_DBAccess_ServiceClient 实例中?


从我在网上找到的一些示例中,他们正在做这样的事情:

ServiceHost host = new ServiceHost(typeof(MyService));
ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
{
    MaxConcurrentCalls = 40,
    MaxConcurrentInstances = 20,
    MaxConcurrentSessions = 20,
};
host.Description.Behaviors.Add(throttleBehavior);
host.Open();

请注意,在上面的代码中,他们使用ServiceHost 而我没有,然后他们打开它(使用Open()),而我打开MusicRepo_DBAccess_ServiceClient 实例......这就是让我困惑。

【问题讨论】:

  • 你不能在配置文件中有这个吗?
  • 我需要在没有 app.config 文件的情况下与多个项目共享这个 wcf 服务...这就是我以编程方式构建配置的原因
  • 您在哪里托管服务?

标签: c# wcf web-services throttling


【解决方案1】:

对于像我这样在运行时进行配置的人来说,可以在代码中完成。

vb 版本:

    Dim stb As New ServiceThrottlingBehavior
    stb.MaxConcurrentSessions = 100
    stb.MaxConcurrentCalls = 100
    stb.MaxConcurrentInstances = 100
    ServiceHost.Description.Behaviors.Add(stb)

c#版本:

    ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior {
        MaxConcurrentSessions = 100,
        MaxConcurrentCalls = 100,
        MaxConcurrentInstances = 100
    };
    ServiceHost.Description.Behaviors.Add(stb);

【讨论】:

    【解决方案2】:

    限制是服务端(服务器)行为而不是客户端行为

    阿农

    【讨论】:

      【解决方案3】:

      你可以在配置文件afaik中指定行为,生成的客户端会服从,使用行为。

      为简洁起见排除了一些配置部分

      <service 
          behaviorConfiguration="throttleThis" />
      
              <serviceBehaviors>
                  <behavior name="throttleThis">
                      <serviceMetadata httpGetEnabled="True" />
                      <serviceThrottling
                          maxConcurrentCalls="40"
                          maxConcurrentInstances="20"
                          maxConcurrentSessions="20"/>
                  </behavior>
              </serviceBehaviors>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多