barrypicker barrypicker - 1 month ago 9
C# Question

How to tell failed method call parameter values?

I have a

List<Task<searchDataResponse>>
that contains about 30 entries. When debugging this List I cannot determine which of the 30 entries is a specific entry.

How do I determine the method call (including input parameter value) of a specific task within a collection of tasks?

Example code:

var tasks = new List<Task<searchDataResponse>>();

for (var i = 0; i < 30; i++)
{
// GET DATE OF N DAYS PAST (UP TO 30)
var searchDate = DateTime.Now.AddDays(i * -1);

// CALL WEB SERVICE WITH A SPECIFIC DATE
var dateSpecificTask = wsClient.searchDataAsync(searchDate);

// ADD TASK TO LIST OF TASKS
tasks.Add(dateSpecificTask);
}

try
{
Task.WaitAll(tasks.ToArray());
}
catch (AggregateException ae)
{
// SET ALL EXCEPTIONS AS HANDLED
ae.Handle(x => { return true; });
}

// GET LIST OF FAILED TASKS
var failedTasks = tasks.Where(x => x.Status == TaskStatus.Faulted).ToList();

// IDENTIFY FAILED TASKS - TRY TO IDENTIFY WHICH DATE SUPPLIED TO WEB SERVICE FAILED.
foreach (var failedTask in failedTasks)
{
var failedMethod = failedTask.MethodSignature; //<---- here is what i want to do. Property 'MethodSignature' does not exist - this is merely an example of what I am attempting to do.
}


UPDATE -
Some of the comments suggest people having heartburn over the use of the phrase Method Signature. What I mean by method signature is actually method signature including input parameter values. As such I have altered the question title to reflect more accurately what I am seeking. My example it contrived and simple. It does not reflect my real-world problem. But my question remains - when viewing the list of failed tasks how do I determine the actual method invoked as part of the task. I cannot seem to find any properties of the task that describe what method caused the failure.

usr usr
Answer
var operations = Enumerable.Range(0, 30)
 .Select(i => {
     // GET DATE OF N DAYS PAST (UP TO 30)
    var searchDate = DateTime.Now.AddDays(i * -1);

    // CALL WEB SERVICE WITH A SPECIFIC DATE
    var dateSpecificTask = wsClient.searchDataAsync(searchDate);

    return new { i, searchDate, dateSpecificTask };
}.ToList();

Task.WaitAll(operations.ConvertAll(x => x.dateSpecificTask));

var failed = operation.Where(x => x.dateSpecificTask.State == Faulted).ToList();

And in the failed list you now have all dates and indexes.