Creating an Ninject interceptor method asynchronously

I use Ninject Interceptor to perform some tasks before and after calling the actual method, but I need these operations for asynchrony. I will review the following article making-ninject-interceptors-work-with-async-methods and implement this part of async, but now I will skip one last part and wait / non-blocking wait for the task to be completed in the Intercept method.

  • I cannot use wait because I want it to be an asynchronous non-blocking operation

    /// <summary> /// Intercepts the specified invocation. /// </summary> /// <param name="invocation">The invocation to intercept.</param> public void Intercept(IInvocation invocation) { Task<bool> resultTask = InterceptAsync(invocation); if (resultTask.Exception != null) throw new Exception("Exception.", resultTask.Exception.InnerException); } /// <summary> /// Intercepts the specified invocation. /// </summary> /// <param name="invocation">The invocation to intercept.</param> protected async Task<bool> InterceptAsync(IMyInvocation invocation) { await BeforeInvokeAsync(invocation); if (!invocation.Cancel) { invocation.Proceed(); await AfterInvokeAsync(invocation); } return true; } 
  • I even tried to put an asynchronous method, and I still have problems, perhaps with the fact that it is a void method

     /// <summary> /// Intercepts the specified invocation. /// </summary> /// <param name="invocation">The invocation to intercept.</param> public async void Intercept(IInvocation invocation) { Task<bool> resultTask = InterceptAsync(invocation); await resultTask; if (resultTask.Exception != null) throw new Exception("Exception.", resultTask.Exception.InnerException); } 

Is there a way to make this real asynchronous method complete?

+4
source share
1 answer

I had to abandon this problem, and I changed a few bits of code inside Ninject.Extensions.Interception to allow async / await .

I just started testing the code, and it still seems that the wait before Proceed is called works. I am not 100% sure that everything works as expected, because I need more time to play with this, so feel free to check the implementation and come back to me if you find errors or suggestions.

https://github.com/khorvat/ninject.extensions.interception

IMPORTANT - this solution only works with LinFu DynamicProxy, since LinFu generates proxy classes that can be used for asynchronous waiting.

Note: again, this solution is a “hack”, and not a complete asynchronous implementation of interception.

Hello

+1
source

Source: https://habr.com/ru/post/1491520/


All Articles