Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have WCF service which is returning start and end time of a method in list of string. I have set Concurrency mode to multiple and InstanceContextMode=PerCall. When i call the method of service using await from the client application, Both request are not executed in parallel. Please find the code of service and client below:

Service Code
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,InstanceContextMode =InstanceContextMode.PerCall)]
    public class Service1 : IService1
    {
        public List<string> GetData(int value)
        {
            List<string> lst = new List<string>();
            DateTime dt1 = DateTime.Now;
            lst.Add("Request :"+value+"---Start Time ----"+dt1.Hour + "_" + dt1.Minute + "_" + dt1.Second + "_" + dt1.Millisecond);
            Thread.Sleep(2000);
             dt1 = DateTime.Now;
            lst.Add("Request :" + value + "---End Time ----"+dt1.Hour + "_" + dt1.Minute + "_" + dt1.Second + "_" + dt1.Millisecond);
            return lst;
        }

        
    }


Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestWCFConClient
{
    public class MyClient
    {
        public async void Do()
        {
            List<Task> lsttask = new List<Task>();
            DateTime dtf = DateTime.Now;
            Console.WriteLine("Execution Begin " + dtf.Hour + "_" + dtf.Minute + dtf.Second + "_" + dtf.Millisecond);
            // ThreadPool.SetMaxThreads(8,8);
            //ThreadPool.SetMinThreads(8, 8);
            //int c;
            //int io;
            //ThreadPool.GetMaxThreads(out c, out io);
            List<string[]> lststr = new List<string[]>();
            ServiceReference1.Service1Client objClient = new ServiceReference1.Service1Client();

            for (int i = 0; i < 2; i++)
            {
                try
                {
                    DateTime dtCurrent = DateTime.Now;
                    int current = i + 1;
                   
                    var objTask = await Task<string[]>.Run(() =>
                    {

                      
                        Task<string[]> obj = objClient.GetDataAsync(current);
                      for(int a=0;a<obj.Result.Length;a++ )

                        {
                            Console.WriteLine(current + obj.Result[a]);
                        }
                        return obj;
                    });
                    lststr.Add(objTask);
                    //  Thread.Sleep(100);
                }
                catch (Exception ex)
                {

                }

            }
            DateTime dte = DateTime.Now;
            Console.WriteLine("Execution End " + dte.Hour + "_" + dte.Minute + "_" + dte.Second + "_" + dte.Millisecond);

          
        
        }
    }
}


Output
Execution Begin 22_5321_403
1Request :1---Start Time ----22_53_22_703
1Request :1---End Time ----22_53_24_704
2Request :2---Start Time ----22_53_24_798
2Request :2---End Time ----22_53_26_799
Execution End 22_53_26_810


I need request 1 and 2 both should start at same time

What I have tried:

I have tried the above solution but it is not giving the expected result
Posted
Updated 4-Mar-19 22:40pm

1 solution

Await says "don't continue until I finish". How do you expect to "loop" under those conditions?

Write a simple parallel program using Parallel.ForEach | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900