Hello,
We are trying to create simple example of WCF service with multiple parallel calls to the same method of WCF. But we faced with some problems:
1. We are calculating time for getting result from WCF service method, and we found that between multiple parallel calls (which are calling WCF at the same time) we have delay between executing methods (it looks like they are executing synchronously or with some delay between calls)
2. Also, we can’t execute more than 35-40 threads in parallel – after that we got an exception
“Can’t connect to endpoint net.tcp://localhost:9002/CalcService. Error code TCP 10061”
3. Also, in our code there is no matter which value has parameter stb.MaxConcurrentCalls – every time we can make no more than 10 calls.
Below there are some parts of code from client and WCF service:
CalcService.cs:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple,
IncludeExceptionDetailInFaults = true, MaxItemsInObjectGraph = int.MaxValue)]
public class CalcService : ICalcService
{
#region ICalcService Members
public DateTime Foo(long id, DateTime dateTime)
{
Thread.Sleep(5000);
return DateTime.Now;
}
#endregion
}
FooWinService.cs:
public partial class FooWinService : ServiceBase
{
private ServiceHost m_svcHost;
public FooWinService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
ServicePointManager.DefaultConnectionLimit = 10000;
ContractDescription contractDescription = new ContractDescription("test");
contractDescription.ProtectionLevel = ProtectionLevel.None;
contractDescription.SessionMode = SessionMode.Allowed;
if (m_svcHost != null) m_svcHost.Close();
string strAdrTCP = "net.tcp://localhost:9002/CalcService";
Uri[] adrbase = {new Uri(strAdrTCP)};
m_svcHost = new ServiceHost(typeof (CalcService), adrbase);
var mBehave = new ServiceMetadataBehavior();
mBehave.MetadataExporter.ExportContract(contractDescription);
m_svcHost.Description.Behaviors.Add(mBehave);
var stb = m_svcHost.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (stb == null)
{
stb = new ServiceThrottlingBehavior();
m_svcHost.Description.Behaviors.Add(stb);
}
stb.MaxConcurrentSessions = 1000;
stb.MaxConcurrentCalls = 1000;
stb.MaxConcurrentInstances = 1000;
var sba = m_svcHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
if (sba == null)
{
sba = new ServiceBehaviorAttribute();
m_svcHost.Description.Behaviors.Add(sba);
}
sba.MaxItemsInObjectGraph = int.MaxValue;
sba.InstanceContextMode = InstanceContextMode.PerCall;
sba.ConcurrencyMode = ConcurrencyMode.Multiple;
var tcpb = new NetTcpBinding();
m_svcHost.AddServiceEndpoint(typeof (ICalcService), tcpb, strAdrTCP);
m_svcHost.AddServiceEndpoint(typeof (IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(),
"mex");
m_svcHost.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.None;
m_svcHost.Open();
}
protected override void OnStop()
{
if (m_svcHost != null)
{
m_svcHost.Close();
m_svcHost = null;
}
}
}
and Client:
private static void Main(string[] args)
{
try
{
Console.WriteLine("Using TCP Binding");
ServicePointManager.DefaultConnectionLimit = 10000;
for (int i = 1; i <= 35; i++)
{
int id = i;
var thread = new Thread(() =>
{
var objCalcClient1 =
new CalcServiceClient("NetTcpBinding_ICalcService");
var binding = (NetTcpBinding)objCalcClient1.Endpoint.Binding;
binding.MaxConnections = 10000;
OperationDescriptionCollection operations = objCalcClient1.Endpoint.Contract.Operations;
foreach (OperationDescription operation in operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph =
2147483647;
}
DateTime startTime = DateTime.Now;
objCalcClient1.Open();
DateTime gettedTime = objCalcClient1.Foo(id, startTime);
TimeSpan timeToPerform = gettedTime - startTime;
Console.WriteLine("Time to get result for id {0} is {1}", id,
timeToPerform);
});
thread.Start();
}
}
catch (Exception eX)
{
Console.WriteLine("There was an error while calling Service [" + eX.Message + "]");
}
Console.WriteLine("I've done it!");
Console.ReadLine();
}
}
So, please, could anybody help us to fix our problems and to show how to work with WCF service in parallel mode with many connections correctly.
Thank you,