none
WCF ConcurrencyMode=Multiple not working RRS feed

  • Question

  • Hi,

       I have created a simple WCF service to test the ConcurrencyMode options.  However, ConcurrencyMode=Multiple has no effect.

    Below is code for my service.

    namespace LongOperationService
    {
        [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
        public class LongOperation : ILongOperation
        {
            public string GetData(int value)
            {
                Thread.Sleep(3000);
                return string.Format(DateTime.Now.ToString() +  " : You entered: {0}", value);
            }
        }
    }

    I have hosted this service using 'WCF Service Host'

    Client code look like this.

    class Program
        {
            private static ServiceReference1.LongOperationClient c = null;
            public static int cnt = 0;
            static void Main(string[] args)
            {
                c = new ServiceReference1.LongOperationClient();
                for (int i = 0; i < 5; i++)
                {
                    ///Event though call are dispatched on different threads
                    ///these calls are queued in on WCF server side.
                    DispatchThread();
                }
                ///==========================================================================================
                ///                                O U T P U T 
                ///==========================================================================================
                ///6/4/2013 2:30:16 PM : You entered: 1
                ///6/4/2013 2:30:19 PM : You entered: 4
                ///6/4/2013 2:30:22 PM : You entered: 2
                ///6/4/2013 2:30:25 PM : You entered: 5
                ///6/4/2013 2:30:28 PM : You entered: 3
                ///Press any key to continue . . .
                
            }
            private static void DispatchThread()
            {            
                Thread th = new Thread(new ThreadStart(() =>
                    {
                        Interlocked.Increment(ref cnt);
                        Console.WriteLine(c.GetData(cnt));                    
                    }));
                th.Start();
            }
        }

    Look at the output, even though call to GetData is made through different thread still it took in all 12 sec to complete the call.  My understanding is it should execute all the calls in 3 seconds as GetData() is executing concurrently.

    Note: I have also tried adding UseSynchronizationContext=false while describing ServiceBehavio

    Can any one help?

    Regards,

    Hemant


    Regards, Hemant Shelar


    • Edited by Hemant.Shelar Tuesday, June 4, 2013 9:13 AM Added extra point
    Tuesday, June 4, 2013 9:08 AM

Answers

  • Ciao Hemant.Shelar,

    You wrote on 04/06/2013 :

    The only difference is its return type is void and its IsOneway attribute is set to ture.  For this method everything is working as expected.

    it is quite obvious since it is one-way, try to fire-up multiple clients calling the same service, using the GetData operation.

    .m



    blog @ //milestone.topics.it
    Tuesday, June 4, 2013 11:24 AM

All replies

  • Ciao Hemant.Shelar,

    You wrote on 04/06/2013 :

    Can any one help?

    add to output of GetData the server side "managed thread id" and post the output once again.

    .m



    blog @ //milestone.topics.it
    Tuesday, June 4, 2013 9:23 AM
  • Hi,

    After adding logging my GetData method look like this.

    public string GetData(int value)
            {
                string strMessage = string.Format("Operation{0} started @ {1} and finished @ ", value, DateTime.Now);
                Thread.Sleep(3000);
                strMessage = strMessage + DateTime.Now + "\n\r";
                strMessage = strMessage + string.Format(DateTime.Now.ToString() +  " : You entered: {0} and Managed Thread id is {1}", value,Thread.CurrentThread.ManagedThreadId);
                return strMessage;
            }

    And   here is the output.  I can see operation on WCF side is dispatched on different threads but still can't understand what is causing sequential execution.

    Operation1 started @ 6/4/2013 3:01:57 PM and finished @ 6/4/2013 3:02:00 PM
    6/4/2013 3:02:00 PM : You entered: 1 and Managed Thread id is 10

    Operation4 started @ 6/4/2013 3:02:00 PM and finished @ 6/4/2013 3:02:03 PM
    6/4/2013 3:02:03 PM : You entered: 4 and Managed Thread id is 8

    Operation2 started @ 6/4/2013 3:02:03 PM and finished @ 6/4/2013 3:02:06 PM
    6/4/2013 3:02:06 PM : You entered: 2 and Managed Thread id is 10

    Operation3 started @ 6/4/2013 3:02:06 PM and finished @ 6/4/2013 3:02:09 PM
    6/4/2013 3:02:09 PM : You entered: 3 and Managed Thread id is 8

    Operation5 started @ 6/4/2013 3:02:09 PM and finished @ 6/4/2013 3:02:12 PM
    6/4/2013 3:02:12 PM : You entered: 5 and Managed Thread id is 10

    Press any key to continue . . .


    Regards, Hemant Shelar

    Tuesday, June 4, 2013 9:37 AM
  • Ciao Hemant.Shelar,

    You wrote on 04/06/2013 :

    And   here is the output.  I can see operation on WCF side is dispatched on different threads but still can't understand what is causing sequential execution.

    lots of different factors can influence that:
    - an attached debugger;
    - release vs debug compilation;
    - current status of your machine running the code;
    - hardware, e.g. if you have 1 CPU with 1 core there is no real parallelism that can be run;

    .m



    blog @ //milestone.topics.it
    Tuesday, June 4, 2013 9:40 AM
  • Hi,

       Thanks.  But I thank problem is some thing else.  Jut to try out different things I added one more method tho the Service which look like this

    public void TestMethod(int value)
            {            
                string strMessage = string.Format("Operation{0} started @ {1} and finished @ ", value, DateTime.Now);
                Thread.Sleep(3000);
                strMessage = strMessage + DateTime.Now;        
                Console.WriteLine(strMessage);
            }

    The only difference is its return type is void and its IsOneway attribute is set to ture.  For this method everything is working as expected.

    Here is the output....

    Operation4 started @ 6/4/2013 3:28:38 PM and finished @ 6/4/2013 3:28:41 PM

    Operation1 started @ 6/4/2013 3:28:38 PM and finished @ 6/4/2013 3:28:41 PM

    Operation5 started @ 6/4/2013 3:28:39 PM and finished @ 6/4/2013 3:28:42 PM

    Operation3 started @ 6/4/2013 3:28:40 PM and finished @ 6/4/2013 3:28:43 PM

    Operation2 started @ 6/4/2013 3:28:40 PM and finished @ 6/4/2013 3:28:43 PM

    Regards,

    Hemant


    Regards, Hemant Shelar

    Tuesday, June 4, 2013 10:04 AM
  • Ciao Hemant.Shelar,

    You wrote on 04/06/2013 :

    The only difference is its return type is void and its IsOneway attribute is set to ture.  For this method everything is working as expected.

    it is quite obvious since it is one-way, try to fire-up multiple clients calling the same service, using the GetData operation.

    .m



    blog @ //milestone.topics.it
    Tuesday, June 4, 2013 11:24 AM