Skip to content

Intercept async method blocks the thread #90

@303248153

Description

@303248153

Test code:

using System;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;

namespace Program
{
    public class Program
    {
        public class Intercept : AbstractInterceptorAttribute
        {
            public override Task Invoke(AspectContext context, AspectDelegate next)
            {
                context.Parameters[0] = "lemon";
                return context.Invoke(next);
            }
        }

        public interface IService
        {
            Task<string> GetValue(string val);
        }

        public class Service : IService
        {
            [Intercept]
            public async Task<string> GetValue(string val)
            {
                await Task.Delay(3000);
                return val;
            }
        }

        static void Main(string[] args)
        {
            var builder = new ProxyGeneratorBuilder();
            builder.Configure(_ => { });
            var proxyGenerator = builder.Build();
            var proxy = proxyGenerator.CreateInterfaceProxy<IService, Service>();
            // IService proxy = new Service();
            var val = proxy.GetValue("le");
            Console.WriteLine("should return immediately");
            Console.WriteLine(val.Result);
        }
    }
}

Should print "should return immediately" immediately after GetValue, then sleep 3s and print the final result.

Problem lines:




AspectContextRuntimeExtensions.AwaitIfAsync(this, returnValue);

return typeof(Task).GetTypeInfo().IsTaskWithResult();

Reference:
https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions