pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
742 stars 111 forks source link

Issue with async #186

Closed waldenltd closed 2 years ago

waldenltd commented 2 years ago

Hello,

Using a sample from another issue I created this

  [Injection(typeof(CatchErrorsAttribute))]
    [Aspect(Scope.Global)]
    public class CatchErrorsAttribute : Attribute
    {
        [Advice(Kind.Around)]
        public object Around(
            [Argument(Source.Arguments)] object[] args,
            [Argument(Source.Target)] Func<object[], object> target)
        {
            try
            {
                return target(args);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
    }

and this

 [CatchErrors] //you may apply it do method directly 
        private void DoSomething()
        {
            throw new Exception("This is some exception!!!");

        }

When executed the Console.WriteLine was hit successfully. When async was added to DoSomething Console.WriteLine is not hit.

pamidur commented 2 years ago

This is expected behaviour. When you exec return target(args) you basically finish the catch block when async method is not finished. Please see this discussion here to have working solution https://github.com/pamidur/aspect-injector/discussions/185

waldenltd commented 2 years ago

Thank you for the update. With your suggestion I was able to create a solution that works for async calls.

pamidur commented 2 years ago

Glad I managed to help @waldenltd Thank you you for your interest in this project