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>

  • 相关阅读:
    在DNN站点中加上Tabs效果,并实现在tab中添加模块
    真正的机器人操作系统Android
    将使用AJAX的DNN模块部署到.net 2.0站点上的问题
    顶级企业招聘N多技术人员,有信心的请来看看,薪资不错
    {欢迎加入成都程序员QQ群82041229}
    用C#编程实现检查Scroll Lock键的状态
    成都程序员求职、招聘QQ群29359494 欢迎加入
    帮朋友发个“招10年以上开发经验的技术人员”
    vs2008中,不能将控件拖到页面上之问题解决
    DNN中使用XSD dataset之tableadapter出错
  • 原文地址:https://www.cnblogs.com/rock_chen/p/793588.html
Copyright © 2011-2022 走看看