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

Created on 25 Jun 2017  路  9Comments  路  Source: autofac/Autofac

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

Most helpful comment

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 https://github.com/castleproject/Core/issues/107 about that. Take a look, might be helpful.

All 9 comments

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 https://github.com/castleproject/Core/issues/107 about that. Take a look, might be helpful.

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.

+1

Hey guys,

Is there any progress being made on this?

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

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

https://github.com/castleproject/Core/issues/145

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

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

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("");
    }
Was this page helpful?
0 / 5 - 0 ratings