-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using AsyncInterceptor with Autofac #42
Comments
Hi @blasferatu, You'll want to look at https://github.com/JSkimming/Castle.Core.AsyncInterceptor/blob/master/src/Castle.Core.AsyncInterceptor/ProxyGeneratorExtensions.cs which performs the bridge of accepting Please check back if this isn't enough to keep you moving forward and we can try to provide an answer. |
(Specifically, I think you'll want to use |
Hi @ndrwrbgs, I created a class named Finally, I registered everything with Autofac:
Finally, the registration of a class for interception:
I think I got it right. I attached files with the code. It might be helpful for someone with the same doubts as me. Thank you very much for your help and guidance. |
Hi @blasferatu , @ndrwrbgs I tried the above-mentioned method to implement an async interceptor. Interceptor has two purposes: to log execution information and to add the result to the cache. Implementation behaves strangely, interceptor method is called multiple (I can not guess how many) times. I created a simple console application. ITestService.Run is the method being intercepted. UniversalInterceptorAsync is the interceptor. Could you please tell what I am doing wrong? |
Hi @tornike87, I executed your code several times and the interceptor method was always called only once. Here is an example of the result in the console:
I think this is the behaviour you want but are getting multiple results like the one above in a single execution. Method I didn't find anything wrong with your code. I compared your implementation with mine and both seem right to me. I am sorry I have been unable to reproduce your problem and can't be more helpful. Best regards. |
@blasferatu that is the behaviour I want, but it doesn't work that way always. We have the project where we need to use this interceptor and when we run it on some PC-s it does not finish writing log several minutes. Can you vlean and debug it with visual studio? When I click F11 on invocation.proceed() it goes back to universal interceptor, makes several loops. If you can not reproduce it I will try to play with it and make better example of this behaviour. Thank you very much for your help. |
Hello @tornike87, I cleaned the solution and executed your code repeatedly in Visual Studio 2017 Professional version 15.7.2 with breakpoints in class When the code runs, I have done this several times during the day and everything has worked correctly. I will keep trying and will let you know if I find anything new about this. I do hope you can find what's wrong because I've not been successful in trying to help you. Best regards. |
@blasferatu I made a simple change, inserted Thank you very much for your time and effort! |
I have exactly the same behaviour you describe when I run the latest code sample you provided. When running again your first code sample, I found that
The only difference I see between your code and mine in the interceptor methods (e.g. Best regards. |
@blasferatu You are absolutely right if I change Async methods when accessing the cache and use synchronous ones, the problem does not happen. I just want methods accessing the cache to be async and I wonder if it is possible. Also for some interceptors, I want to have some logic accessing some web services or other external resources and I want to know if it is possible to call async methods. Could it be related to the autofac registration or is it a general problem with this library? Thank you very much! |
I am afraid I don't know the answer to your question. I can only add that in my interception methods I do have asynchronous code after I wish someone more knowledgeable than me could provide more insight into this matter. Best regards. |
Hey folks, sorry, I’ve been busy and this was on my TODO list.
Indeed the library does not work today if you have any await before the
await proceed, that has to be the first await due to constraints in Castle.
James and I have some discussions going over there (and perhaps in issues
here? I’m mobile so cannot link them) regarding this but today you cannot
await operations until after you await proceed.
…On Fri, Jun 1, 2018 at 10:08 AM blasferatu ***@***.***> wrote:
@tornike87 <https://github.com/tornike87>,
I am afraid I don't know the answer to your question.
I can only add that in my interception methods I do have asynchronous code
*after* invocation.Proceed();.
I wish someone more knowledgeable than me could provide more insight into
this matter.
Best regards.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#42 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AKRxOoXxvQfGQTiRq-4sZHUZVqhgzT6Eks5t4XTvgaJpZM4T20Oj>
.
|
Here are some suggestions I’ve used successfully with this limitation while we push for changes in Castle.
|
@ndrwrbgs, |
@ndrwrbgs, @blasferatu Thank you very much. @ndrwrbgs Is there any thread regarding this problem in Castle? Best regards. |
Relevant threads: Last update was we are really really hoping the Castle experts can make Async first-class support, though I believe they're hoping we can do it :( |
see this sample for autofac async interceptor with the help of AsyncDeterminationInterceptor and AsyncInterceptorBase:
public class AsyncInterceptorAdaper<TAsyncInterceptor> : AsyncDeterminationInterceptor
where TAsyncInterceptor : IAsyncInterceptor
{
public AsyncInterceptorAdaper(TAsyncInterceptor asyncInterceptor)
: base(asyncInterceptor)
{ }
}
public class CallLoggerAsyncInterceptor : AsyncInterceptorBase
{
....
}
[Intercept(typeof(AsyncInterceptorAdaper<CallLoggerAsyncInterceptor>))]
public interface ISomeType
//register adapter
builder.RegisterGeneric(typeof(AsyncInterceptorAdaper<>));
//register async interceptor
builder.Register(c => new CallLoggerAsyncInterceptor(Console.Out)); |
how to asyncinterceptor for class ? |
here is the demo code: |
It should be ok if inherit AsyncInterceptorBase, IInterceptor at the same time. then just simply Intercept as below: public void Intercept(IInvocation invocation)
{
this.ToInterceptor().Intercept(invocation);
} |
I'm using Autofac and would like to use AsyncInterceptor. However, I cannot register a class that implements IAsyncInterceptor because Autofac expects a class that implements IInterceptor.
From the example in the Readme I don't understand how I could use AsyncInterceptor with Autofac. I thought of using a class that implements IInterceptor decorating a class that implements IAsyncInterceptor.
Then I would register the class implementing IInterceptor with Autofac which, in turn, would use the class implementing IAsyncInterceptor.
Am not sure how to do it or whether it can be done.
Any help would be appreciated.
Thank you.
The text was updated successfully, but these errors were encountered: