zoukankan      html  css  js  c++  java
  • 【WCF】a bunch of concurrent clients!

    原帖地址:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1755618&SiteID=1

     There's an additional setting on the binding that you would need to increase.  It's the ListenBacklog.  I think if you make this greater than or equal to your MaxConcurrentCalls, the service will be more stable.

     By default, all the throttling behaviors (MaxConcurrentCalls/Instances/Sessions) are set to 10.  The ListenBacklog is set to 10 by default, too.  The ListenBacklog defines how many messages to "buffer" while the service tries to dispatch other messages.  So, if you set all the throttling behaviors to something like 5 and set ListenBacklog to something greater than the number of clients you plan to connect, you could still process a bunch of concurrent clients, provided they close their channels after sending the messages.  For reference, here's the code I was working with to understand this:

    Code Snippet
    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.ServiceModel;

    using System.ServiceModel.Description;

    using System.Threading;

    using System.ServiceModel.Channels;

    namespace LotsOfChannels

    {

    class Program

    {

    static void Main(string[] args)

    {

    try

    {

    // channelsToOpen determines how many concurrent threads will be started.

    int channelsToOpen = 20;

    string address = "net.tcp://localhost/service";

    NetTcpBinding binding 
    = new NetTcpBinding("mathTCPBindingConf");

    binding.ListenBacklog 
    = 10;

    ServiceHost host 
    = new ServiceHost(typeof(HelloService), new Uri(address));

    host.AddServiceEndpoint(
    typeof(IHelloContract), binding, address);

    // I think that ListenBacklog + MaxConcurrentCalls > channelsToOpen. 

    // Okay, maybe that's not entirely true, because it doesn't count the messages that have already been dispatched. Maybe a better way to test this is to have the service method just sleep. Then I can count the number of calls waiting/buffered/hanging/etc.

    // The most reliable thing I've found is that ListenBackLog >= MaxConcurrentCalls generates more stable services.

    ServiceThrottlingBehavior throttling 
    = new ServiceThrottlingBehavior();

    throttling.MaxConcurrentCalls 
    = 10;

    throttling.MaxConcurrentInstances 
    = 5;

    throttling.MaxConcurrentSessions 
    = 5;

    ServiceDescription des 
    = host.Description;


    des.Behaviors.Add(throttling);

    host.Open();

    // Now connect clients concurrently:

    Thread[] channelThreads 
    = new Thread[channelsToOpen];

    for (int i = 0; i < channelsToOpen; ++i)

    {

    channelThreads[i] 
    = new Thread(new ParameterizedThreadStart(SendMessageOnChannel));

    channelThreads[i].Name 
    = "Thread #" + i;

    }

    for (int i = 0; i < channelsToOpen; ++i)

    {

    ChannelFactory
    <IHelloContract> factory = new ChannelFactory<IHelloContract>(binding, address);

    IHelloContract channel 
    = factory.CreateChannel();

    channelThreads[i].Start((Object)channel);

    }

     

     

    Console.WriteLine(
    "Done.");

    }

    catch (Exception e)

    {

    Console.WriteLine(e);

    }

    Console.ReadLine();

    }

    public static void SendMessageOnChannel(Object channel)

    {

    try

    {

    Console.WriteLine(
    "Sending message on {0}", Thread.CurrentThread.Name);

    ((IHelloContract)channel).Hello(Thread.CurrentThread.Name);

    ((IChannel)(IHelloContract)channel).Close();

    }

    catch (Exception e)

    {

    Console.WriteLine(e);

    }

    }

     

    }

     

     

    [ServiceContract]

    public interface IHelloContract

    {

    [OperationContract]

    void Hello(string name);

    }

    public class HelloService : IHelloContract

    {

    public void Hello(string name)

    {

    Console.WriteLine(
    "[Service]: Hello. Received from thread {0}", name);

    }

    }

    }


    The App.config file just has the binding you used

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    <system.serviceModel>

    <bindings>

    <netTcpBinding>

    <binding name="mathTCPBindingConf" 

    closeTimeout
    ="00:00:10" 

    openTimeout
    ="00:10:00"

    receiveTimeout
    ="00:10:00" 

    sendTimeout
    ="00:10:00" 

    maxConnections
    ="500" 

    maxBufferSize
    ="104857600" 

    maxReceivedMessageSize
    ="104857600">

    <security mode="None" />

    <readerQuotas maxDepth="2147483647"

    maxStringContentLength
    ="2147483647"

    maxArrayLength
    ="2147483647"

    maxBytesPerRead
    ="2147483647"

    maxNameTableCharCount
    ="2147483647" />

    </binding>

    </netTcpBinding>

    </bindings>

    <behaviors>

    <serviceBehaviors>

    <behavior name="MathServiceBehaviours" >

    <serviceThrottling

    maxConcurrentCalls="100"

    maxConcurrentSessions
    ="100"

    maxConcurrentInstances
    ="100"

    />

    </behavior>

    </serviceBehaviors>

    </behaviors>

    </system.serviceModel>

    </configuration>

  • 相关阅读:
    lua类对象
    toLua初始化碰到的问题
    Unity经验之谈-DoTween动画结束匿名委托闭包之坑
    toLua关于委托没有注册的解决方案
    xLua使用require改变路径加载Lua脚本
    unity常用的比较函数
    Shader中颜色混合的算法
    UnityShader中插值平滑曲线
    Shader中的Uniforms(只读标识)
    ShaderLab中Properties语义块支持的属性类型
  • 原文地址:https://www.cnblogs.com/rock_chen/p/793588.html
Copyright © 2011-2022 走看看