Skip to content
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

how to aply Autofac.Extras.DynamicProxy to a async method??? #856

Closed
wangfengjun404 opened this issue Jun 25, 2017 · 9 comments
Closed

how to aply Autofac.Extras.DynamicProxy to a async method??? #856

wangfengjun404 opened this issue Jun 25, 2017 · 9 comments

Comments

@wangfengjun404
Copy link

how to aply Autofac.Extras.DynamicProxy to a async method???

invocation.Proceed() is a void method that i can not await

is there anybody can help me with this problem?

thanks a lot

@osoykan
Copy link

osoykan commented Jun 25, 2017

I think this is related with Castle.Core not Autofac and AFAIK there is no async/await support for interceptors.

There is an issue in castleproject/Core#107 about that. Take a look, might be helpful.

@tillig
Copy link
Member

tillig commented Jun 25, 2017

Yup, this is a Castle question. Autofac can attach an interceptor for you, but the actual interception and how to implement the interceptors themselves is a Castle.Core thing. The issue mentioned by @osoykan is a great start on that.

@tillig tillig closed this as completed Jun 25, 2017
@moattarwork
Copy link

+1

@alex-training
Copy link

Hey guys,

Is there any progress being made on this?

@tillig
Copy link
Member

tillig commented Feb 12, 2018

As noted above this is a Castle question, not Autofac.

@alex-training
Copy link

Thanks, @tillig,
Unfortunately, I do not see any movement on that side...

castleproject/Core#145

@tillig
Copy link
Member

tillig commented Feb 12, 2018

I would recommend pinging there. There is, quite literally, nothing that can be done on this from the Autofac side.

@almarax
Copy link

almarax commented Mar 24, 2018

Actually you can workaround it inside interceptor, here is an amusing workaround
https://stackoverflow.com/questions/32994263/autofac-interception-async-execution-order

@sashafp10
Copy link

This worked for me:

    public void Intercept(IInvocation invocation)
    {
        _output.WriteLine("    - Calling method {0} with parameters {1}... ",
            invocation.Method.Name,
            string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));

        var attrs = invocation.MethodInvocationTarget.GetCustomAttributes(typeof(CacheParamsAttribute), false);
        var options = new CacheParamsAttribute();

        if (attrs.Any())
            options = attrs.First() as CacheParamsAttribute;

        _output.WriteLine($"Attributes: {(string.Join(", ", attrs.Select(JsonConvert.SerializeObject).ToList()))}");

        invocation.Proceed();

        if (options.IsAsync)
        {
            var resultMethod = invocation.ReturnValue.GetType().GetMethods().FirstOrDefault(n => n.Name == "get_Result");
            var res = resultMethod?.Invoke(invocation.ReturnValue, null);
            _output.WriteLine("    - Done: result was {0}.", res);
        }
        else
            _output.WriteLine("    - Done: result was {0}.", invocation.ReturnValue);
        _output.WriteLine("");
        _output.WriteLine("");
        _output.WriteLine("");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants