Esempio n. 1
0
        private static Func <object, CancellationToken, Task <object> > CreateInvocationDelegate <T>(
            MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            ParameterExpression request = Expression.Parameter(typeof(object), "request");
            ParameterExpression cancel  = Expression.Parameter(typeof(CancellationToken), "cancel");
            Func <object, CancellationToken, Task <T> > lambda = Expression.Lambda <Func <object, CancellationToken, Task <T> > >(
                serviceMethod.NumberOfParameters() == 1?
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request):
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request, cancel),
                request,
                cancel).Compile();

            return(async(r, c) => await lambda(r, c));
        }
        public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            if (!this.CanCreateInvocationDelegateFor(serviceMethod))
            {
                throw new ArgumentException(
                          "Method " + serviceMethod + " should have a single parameter, be marked as async or return a Task, and should not be static",
                          "serviceMethod");
            }

            ParameterExpression request = Expression.Parameter(typeof(object), "request");
            ParameterExpression cancel  = Expression.Parameter(typeof(CancellationToken), "cancel");
            Action <object, CancellationToken> lambda = Expression.Lambda <Action <object, CancellationToken> >(
                serviceMethod.NumberOfParameters() == 1?
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request):
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request, cancel),
                request,
                cancel).Compile();

            return(new Func <object, CancellationToken, Task <object> >((r, c) =>
            {
                lambda(r, c);
                return TaskEx.FromResult(context.DefaultResponse);
            }));
        }