Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:43:48+00:00 2026-06-16T04:43:48+00:00

I am trying understand concurrency in wcf and downloaded a sample code from here

  • 0

I am trying understand concurrency in wcf and downloaded a sample code from here and done some changes for testing two way calls. To my surprise I could not see the effect of concurrency for two way calls (Although I can see concurrency for one way calls).
Is it the way WCF concurrency model works? (or)
Am I doing something terribly wrong?

This is the service code.

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract(IsOneWay=true)]
    void Call(string ClientName);

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HelloWorldService : IHelloWorldService
{
    public static int counter;

    public HelloWorldService()
    {
        counter++;
    }

    public void Call(string ClientName)
    {            
        Console.WriteLine("Instance:" + counter.ToString() + " Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() + " Time:" + DateTime.Now.ToString() + "\n\n");
        Thread.Sleep(5000);
    }

    public string GetData(int value)
    {
        Console.WriteLine("Instance:" + counter.ToString() + " Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() + " Time:" + DateTime.Now.ToString() + "\n\n");
        Thread.Sleep(5000);
        return value.ToString();
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        Console.WriteLine("Instance:" + counter.ToString() + " Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() + " Time:" + DateTime.Now.ToString() + "\n\n");
        Thread.Sleep(5000);
        return composite;
    }
}

This is the service hosting code.

class Program
{
    static void Main(string[] args)
    {
        //Create a URI to serve as the base address

        //Uri httpUrl = new Uri("net.tcp://localhost:8001/HelloWorld");
        Uri httpUrl = new Uri("http://localhost:8010/MyService/HelloWorld");

        //Create ServiceHost
        ServiceHost host
        = new ServiceHost(typeof(ClassLibrary1.HelloWorldService), httpUrl);

        //Add a service endpoint
        host.AddServiceEndpoint(typeof(ClassLibrary1.IHelloWorldService)
        , new WSHttpBinding(), "");

        //Enable metadata exchange
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        host.Description.Behaviors.Add(smb);

        ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior();
        stb.MaxConcurrentCalls = 100;
        stb.MaxConcurrentInstances = 100;
        stb.MaxConcurrentSessions = 100;
        host.Description.Behaviors.Add(stb);


        //Start the Service
        host.Open();

        Console.WriteLine("Service is host at " + DateTime.Now.ToString());
        Console.WriteLine("Host is running... Press <Enter> key to stop");
        Console.ReadLine();

    }
}

This is the client code.

class Program
{
    static int m_NumberOfWorkers = 10;
    static readonly object m_Locker = new object();
    static bool flag_GO = false;
    static Stopwatch m_OverAllStopwatch = new Stopwatch();
    static ConcurrentBag<Stopwatch> m_bagIndividualStopwatch = new ConcurrentBag<Stopwatch>();
    static int m_CompletedWorkers = 0;
    static ServiceReference1.HelloWorldServiceClient m_objProxy;
    static int m_KindOfMethod;
    static void Main(string[] args)
    {
        while(true)
        {
            try
            {
                flag_GO = false;

                Console.WriteLine("Enter number of concurrent clients:");
                m_NumberOfWorkers = Int32.Parse(Console.ReadLine());

                Console.WriteLine("Kind of method (1: One way, 2: Two way 3: Two way using data contract):");
                m_KindOfMethod = Int32.Parse(Console.ReadLine());

                // Create Workers
                List<Thread> lstThreads = new List<Thread>();
                for (int i = 0; i < m_NumberOfWorkers; ++i)
                {
                    lstThreads.Add(new Thread(WaitOnPulse));
                }

                // Start Workers
                for (int i = 0; i < lstThreads.Count; ++i)
                {
                    lstThreads[i].Start();
                }

                m_objProxy = new ServiceReference1.HelloWorldServiceClient();

                m_OverAllStopwatch.Restart();

                // Signal all workers
                lock (m_Locker)
                {
                    flag_GO = true;
                    Monitor.PulseAll(m_Locker);
                }

                // Wait all workers to finish
                for (int i = 0; i < lstThreads.Count; ++i)
                {
                    lstThreads[i].Join();
                }

                m_objProxy.Close();
                m_objProxy = null;
            }
            catch
            {
                return;
            }
        }            
    }

    private static void WaitOnPulse()
    {
        lock (m_Locker)
        {
            while (!flag_GO) Monitor.Wait(m_Locker);
        }
        TestWhatEverYouWant();
        IamDone();
    }

    private static void TestWhatEverYouWant()
    {
        Stopwatch stopWatch = Stopwatch.StartNew();
        //Thread.Sleep(1000);
        switch (m_KindOfMethod)
        {
            case 1:
                m_objProxy.Call(m_NumberOfWorkers.ToString() + "Client Calls");
                break;
            case 2:
                m_objProxy.GetData(m_NumberOfWorkers);
                break;
            case 3:
                ServiceReference1.CompositeType objData = new ServiceReference1.CompositeType();
                m_objProxy.GetDataUsingDataContract(objData);
                break;
        }
        stopWatch.Stop();
        m_bagIndividualStopwatch.Add(stopWatch);
    }

    private static void IamDone()
    {
        Interlocked.Increment(ref m_CompletedWorkers);
        // Summarize results if all workers are done
        if (Interlocked.CompareExchange(ref m_CompletedWorkers, 0, m_NumberOfWorkers) == m_NumberOfWorkers)
        {
            m_OverAllStopwatch.Stop();
            Console.WriteLine("OverAll Elapsed Time: {0}", m_OverAllStopwatch.ElapsedMilliseconds);
            Stopwatch stopWatch;
            while (m_bagIndividualStopwatch.TryTake(out stopWatch))
            //foreach (Stopwatch stopWatch in m_bagIndividualStopwatch)
            {
                Console.WriteLine("Individual Elapsed Time: {0}", stopWatch.ElapsedMilliseconds);
            }
        }
    }
}

This is the Cleint trace:

    Enter number of concurrent clients:
    8
    Kind of method (1: One way, 2: Two way 3: Two way using data contract):
    2
    OverAll Elapsed Time: 42022
    Individual Elapsed Time: 42021
    Individual Elapsed Time: 37013
    Individual Elapsed Time: 32008
    Individual Elapsed Time: 26987
    Individual Elapsed Time: 21981
    Individual Elapsed Time: 16980
    Individual Elapsed Time: 11968
    Individual Elapsed Time: 6985

This is the server trace:

    Instance:1 Thread:6 Time:12/17/2012 8:09:29 PM


    Instance:1 Thread:5 Time:12/17/2012 8:09:34 PM


    Instance:1 Thread:7 Time:12/17/2012 8:09:39 PM


    Instance:1 Thread:7 Time:12/17/2012 8:09:44 PM


    Instance:1 Thread:5 Time:12/17/2012 8:09:49 PM


    Instance:1 Thread:7 Time:12/17/2012 8:09:54 PM


    Instance:1 Thread:5 Time:12/17/2012 8:09:59 PM


    Instance:1 Thread:7 Time:12/17/2012 8:10:04 PM

For these results you can clearly see that the requests were processed sequentially. Ideally I was expecting all the 8 concurrent requrest would finish in 5 sec. But it took around 42 sec to finish.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:43:50+00:00Added an answer on June 16, 2026 at 4:43 am

    The problem in my code is the way the proxy is used. I have created just one proxy for all concurrent clients and all calls to the service were made through this proxy only. So, all these calls were getting queued in the channel. Creating one proxy for each client, the way one should simulate wcf load test, solved the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand something here: if I render something to the DOM from javascript,
I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program:
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
HI Trying to understand how __radd__ works. I have the code >>> class X(object):
just trying to understand ObjectiveC a bit better. Shouldn't this two expressions be the
I'm trying to understand what makes the lock in concurrency so important if one
I am trying to understand the proper way of using Grand Central Dispatch (GCD)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.