seesharper / LightInject.Interception

LightInject.Interception supports Aspect Oriented Programming through proxy-based method interceptors.
11 stars 7 forks source link

Intercepting asynchronous methods #4

Closed eugene-chehovskoy closed 8 years ago

eugene-chehovskoy commented 8 years ago

Hello,

I have repository with async methods and a want to create logging interceptor. The problem is that catch block doesn't fire because of async method and I can't await invovationInfo.Proceed() inside Invoke method:

        public object Invoke(IInvocationInfo invocationInfo)
        {
            try
            {
                Debug.WriteLine("Start");
                var returnValue = invocationInfo.Proceed();
                Debug.WriteLine("End");
                return returnValue;
            }
            catch(Exception e)
            {
                Debug.WriteLine("Exception: " + e.Message);
                throw;
            }
        }

So the question is: how to work with async method inside interceptor and how to use the same interceptor with async and non-async methods?

P.S. I've found this https://msdn.microsoft.com/en-us/magazine/dn574805.aspx article about intercepting asynchronous methods using Unity, but it looks quite complicated and how to use this approach with LightInject?

seesharper commented 8 years ago

Interesting. The approach taken in the article is probably not so far from the truth. I will take a look at it and see if there is something there that can be provided as helper classes in LightInject.Interception

eugene-chehovskoy commented 8 years ago

Some more links I've found how it works in other libraries: https://snipt.net/raw/33a8f602a9126cb0dd782ea94103383e/?nice https://github.com/ninject/Ninject.Extensions.Interception/blob/master/src/Ninject.Extensions.Interception/AsyncInterceptor.cs http://blog.cincura.net/233489-injecting-logging-into-asynchronous-methods/ http://stackoverflow.com/questions/28099669/intercept-async-method-that-returns-generic-task-via-dynamicproxy http://stackoverflow.com/questions/13630548/making-ninject-interceptors-work-with-async-methods

Sky4CE commented 8 years ago

Any update on this problem?

We faced with this problem in our project also, we need to intercept async methods, but seems like LightInject doesn't support this. I'm personally like to use LIghtInject and don't want to switch to something else, but at this point it is crucial for us to use proxy/interception feature and because we intensively using async\await we need it to be supported.

So, @seesharper , do you have any plans to implement this feature ?

seesharper commented 8 years ago

In progress right now

seesharper commented 8 years ago

LightInject.Interception v1.2.0 published to the official NuGet feed

http://www.lightinject.net/interception/#asynchronous-interceptors