WCF 连接数

 

WCF 限流 Throttling

限流不是直接的实例管理技术,他允许开发者限制客户端的连接数已经服务器负荷,使用限流技术以后,一旦超出配置的设置值,WCF就会自动的将等待处理的调用放入队列中,然后再依次从队列中取出,如果客户端等待超时,那么客户端会获得一个TimeoutException异常。每个服务类型都可以使用WCf限流技术。

WCF限流配置参数

并发会话最大数:针对TCP,IPC等能保持传输层连接的会话的服务绑定的独立客户端最大数,也就是能保持会话的客户端的最大连接数。对于Http等无连接的服务是无效的,默认为10

并发调用最大数:指所有服务实例中读取正在执行的调用总数

并发实例最大数:表示存活的并发上下文总数。默认是无限的。

配置限流

      <behaviors>
         <serviceBehaviors>
            <behavior name = "ThrottledBehavior">
               <serviceThrottling  
                  maxConcurrentCalls     ="12"
                  maxConcurrentSessions ="34"
                  maxConcurrentInstances ="2"
               />
            </behavior>
         </serviceBehaviors>
      </behaviors>

在服务端读取限流参数

         ChannelDispatcher dispatcher = OperationContext.Current.Host.ChannelDispatchers[0] as ChannelDispatcher;

         ServiceThrottle serviceThrottle = dispatcher.ServiceThrottle;

         Trace.WriteLine("MaxConcurrentCalls = "+ serviceThrottle.MaxConcurrentCalls);
         Trace.WriteLine("MaxSessions = " + serviceThrottle.MaxConcurrentSessions);
         Trace.WriteLine("MaxInstances = " + serviceThrottle.MaxConcurrentInstances);

编程配置限流

宿主进程可以以编程方式配置限流,但是要在打开宿主之前执行

         ServiceHost host = new ServiceHost(typeof(MyService));
         //Make sure there is no throttle in the config file
         
ServiceThrottlingBehavior throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
         if(throttle == null)
         {
            throttle = new ServiceThrottlingBehavior();
            throttle.MaxConcurrentCalls     = 12;
            throttle.MaxConcurrentSessions = 34;
            throttle.MaxConcurrentInstances = 2;
            host.Description.Behaviors.Add(throttle);
         }

         host.Open();

绑定中的限流连接

在使用TCP和IPC绑定的时候我们也可以在绑定中为一个特定的终结点配置最大连接数。

          <netTcpBinding>
            <binding name="TCPThrottle" maxConnections="25"></binding>
          </netTcpBinding>

maxConnections的默认值为10,如果绑定限流与服务行为的限流都设置了最大连接值,WCF选择其中较小的一个

原文地址:https://www.cnblogs.com/yanzhenan/p/3097203.html