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>

  • 相关阅读:
    BZOJ 1021 循环的债务
    BZOJ 1019 汉诺塔
    BZOJ 1018 堵塞的交通
    BZOJ 1017 魔兽地图
    BZOJ 1016 最小生成树计数
    Luogu 3008 [USACO11JAN]道路和飞机Roads and Planes
    Luogu 3625 [APIO2009]采油区域
    Luogu 4139 上帝与集合的正确用法
    Luogu 3629 [APIO2010]巡逻
    Luogu 3626 [APIO2009]会议中心
  • 原文地址:https://www.cnblogs.com/rock_chen/p/793588.html
Copyright © 2011-2022 走看看