Intercept the call to an async method using DynamicProxy

后端 未结 8 988
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 02:30

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

相关标签:
8条回答
  • 2020-12-13 03:16
       void IInterceptor.Intercept(IInvocation invocation) {
           try {
               invocation.Proceed();
               var task = invocation.ReturnValue as Task;
               if (task != null && task.IsFaulted) throw task.Exception;
           }
           catch {
               throw;
           }
       }
    
    0 讨论(0)
  • 2020-12-13 03:17

    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.

    0 讨论(0)
提交回复
热议问题