ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Intercept Async Methods, IInterceptor vs IAsyncInterceptor #272

Open scovel opened 3 years ago

scovel commented 3 years ago

I've been using Grace for some time. Thanks! It has been working really well.

I've been implementing IInterceptor, but as our code gets more and more async, I'm finding that this isn't working. I'm using Castle.Core for interception.

Someone has written an extension for Castle.Core to help simplify Async Interception. Castle.Core.AsyncInterceptor. The type I'm using is IAsyncInterceptor, as mentioned in Option 2.

In order for this to work, there needs to be some support in Grace for the new interface, IAsyncInterceptor.

So 2 questions I guess.

  1. Is there a currently supported way to handle intercepting Async and Synchronous Methods? (example?)
  2. If not, would it be possible to support IAsyncInterceptor in Grace?

Thanks,

Sean

silkfire commented 3 years ago

@ipjohnson Could this be a limitation due to the lack of async support in LINQ expressions (Expression)? Seeing as System.Linq.Expressions have been archived this doesn't look very promising.

https://github.com/dotnet/csharplang/discussions/158 https://github.com/dotnet/runtime/issues/27790

ipjohnson commented 3 years ago

@silkfire is correct that Grace itself can not have async parts in dependency construction.

That said Grace doesn't do any interception itself so it should be possible to plug in any interception library be it sync or async.

What specifically isn't work?

scovel commented 3 years ago

Good question. I guess I've been using it so long I forgot who owns the interception code.

I'm using (an old copy, I just noticed) of this InterceptionExtensions to hook up interception.

It doesn't support the new IAsyncInterceptor interface. Now that I realize where that came from, I can understand your confusion!

InterceptionHandler class implements IAsyncInterceptor. It used to implement IInteceptor.

exportRegistrationBlock.Intercept(interceptInterface);

The constraint on this is for IInterceptor. I guess I need to spend some time on this code, see if I can get it working with IAsyncInterceptor.

I may need some help if I get stuck.

Thanks,

Sean

ipjohnson commented 3 years ago

Let me know if you run into any trouble, my hope is that it's pretty straight forward.