My questions is related to this post Intercept the call to an async method using DynamicProxy
I want to implement interceptor that works with async methods that retu
A better solution would be to use the dynamic keyword to bypass the compiler type checking and resolve the operation at run time:
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
var method = invocation.MethodInvocationTarget;
var isAsync = method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null;
if (isAsync && typeof(Task).IsAssignableFrom(method.ReturnType))
{
invocation.ReturnValue = InterceptAsync((dynamic)invocation.ReturnValue);
}
}
private static async Task InterceptAsync(Task task)
{
await task.ConfigureAwait(false);
// do the continuation work for Task...
}
private static async Task InterceptAsync(Task task)
{
T result = await task.ConfigureAwait(false);
// do the continuation work for Task...
return result;
}