Below is the code from the Intercept
method on a custom type that implements IInterceptor
of the Castle Dynamic Proxy library. This snippet is from
void IInterceptor.Intercept(IInvocation invocation) { try { invocation.Proceed(); var task = invocation.ReturnValue as Task; if (task != null && task.IsFaulted) throw task.Exception; } catch { throw; } }
Having a need to intercept methods returning Task<TResult>
, I've created an extension to Castle.Core
that simplifies the process.
Castle.Core.AsyncInterceptor
The package is available to download on NuGet.
The solution is largely based on this answer from @silas-reinagel, but simplifies it by providing a new interface to implement IAsyncInterceptor. There are also further abstractions to that make interception similar to implementing Interceptor
.
See the readme of the project for further details.