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>

  • 相关阅读:
    doT.js——前端javascript模板引擎问题备忘录
    (转)regex类(个人理解)
    ajax提交表单、ajax实现文件上传
    SQL添加表字段
    Elasticsearch使用总结
    有一张表里面有上百万的数据,在做查询的时候,如何优化?从数据库端,java端和查询语句上回答
    sql语句的字段转成Date
    Mybatis 示例之 foreach
    Eclipse不编译解决方案
    Java使用RSA加密解密及签名校验
  • 原文地址:https://www.cnblogs.com/rock_chen/p/793588.html
Copyright © 2011-2022 走看看