I have implemented a service name ExamClient which have two operations one is Ping which return a basic string which mean service is available and one is FindStudy which search in DB it may take a long to be proceeded.
In the other side I have several endpoints of ExamClient I wand to run FindStudy per end point by task so in a Dispatcher I have something like this:
public FindStudies_DTO_OUT FindStudies(FindStudies_DTO_IN findStudies_DTO_IN)
{
    List<Study_C> ret = new List<Study_C>(); 
    List<Task> tasks = new List<Task>();
    foreach (var sp in Cluster)
    {
        string serviceAddress = sp.GetLibraryAddress(ServiceLibrary_C.PCM) + "/Exam.svc";
        var task = Task.Run(() =>
        {
            ExamClient examClient = new ExamClient(serviceAddress.GetBinding(), new EndpointAddress(serviceAddress), Token);
            var ping = Task.Run(() =>
            {
                examClient.Ping();
            }); 
            if (!ping.Wait(examClient.Endpoint.Binding.OpenTimeout))
            {
                Logging.Log(LoggingMode.Warning, "Timeout on FindStudies for:{0}, address:{1}", sp.Name, serviceAddress);
                return new List<Study_C>(); // if return null then need to manage it on ret.AddRange(t.Result);
            }  
            return (examClient.FindStudies(findStudies_DTO_IN).Studies.Select(x =>
            {
                x.StudyInstanceUID = string.Format("{0}|{1}", sp.Name, x.StudyInstanceUID);
                x.InstitutionName = sp.Name;
                return x;
            }));
        });
        task.ContinueWith(t =>
        {
            lock (ret)
            {
                ret.AddRange(t.Result);
            }
        }, TaskContinuationOptions.OnlyOnRanToCompletion);
        task.ContinueWith(t =>
        {
            Logging.Log(LoggingMode.Error, "FindStudies failed for :{0}, address:{1}, EXP:{2}", sp.Name, serviceAddress, t.Exception.ToString());
        }, TaskContinuationOptions.OnlyOnFaulted);
        tasks.Add(task);
    } 
    try
    {
        Task.WaitAll(tasks.ToArray());
    }
    catch (AggregateException aggEx)
    {
        foreach (Exception exp in aggEx.InnerExceptions)
        {
            Logging.Log(LoggingMode.Error, "Error while FindStudies EXP:{0}", exp.ToString());
        }
    }
    return new FindStudies_DTO_OUT(ret.Sort(findStudies_DTO_IN.SortColumnName, findStudies_DTO_IN.SortOrderBy));
}
First I have to run Ping per end point to know connection is established
after that FindStudy.
if there are three end pints in Cluster six task be run in parallel mode, 3 for Ping and 3 for FindStudy.
I think something is wrong with my code to handle exception nice... So what is the best way to implement this scenario ?
thanks in advance.
 
     
    