0
votes

I'm having trouble with existing code that does not work anymore.

Basically, I'm invoking an asynchronous method by reflection:

Sometimes it does work, sometimes it does not.

The code comes from here

using (var scope = this.services.CreateScope())
{
    var workerType = workOrder
        .GetType()
        .GetInterfaces()
        .First(t => t.IsConstructedGenericType && t.GetGenericTypeDefinition() == typeof(IBackgroundWorkOrder<,>))
        .GetGenericArguments()
        .Last();

    var worker = scope.ServiceProvider
        .GetRequiredService(workerType);

    var task = (Task)workerType
        .GetMethod("DoWork")
        .Invoke(worker, new object[] { workOrder, this.shutdown.Token });

    await task.ContinueWith(HandleTask, TaskScheduler.Current).ConfigureAwait(continueOnCapturedContext: false);
}

In debug mode, If I set a breakpoint ont the last await, and in the async method that should be invoked, I can see than the task is already faulted before the method is called.

What could be wrong here? Any Ideas?

Why use reflection? Of the task faulted, isn't there something in the task, instead of the code calling it? You're not giving a minimal reproducible example, so it's kinda hard to help debug. - JHBonarius