【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 >

转载于:https://www.cnblogs.com/rock_chen/archive/2007/06/22/793588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值