Esempio n. 1
0
        public void Intercept(IInvocation invocation)
        {
            RmiResponseDto responseDto = null;
            var            method      = invocation.Method;

            var executorTask = Go(async() => {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"At intercept async for invocation on method {method.Name} for service {remoteServiceInfo.ServiceType.Name}");
                }
                responseDto = await InterceptAsync(method, invocation.Arguments).ConfigureAwait(false);
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Completing Intercept async for invocation on method {method.Name} for service {remoteServiceInfo.ServiceType.Name}");
                }
            });

            if (typeof(Task).IsAssignableFrom(method.ReturnType))
            {
                invocation.ReturnValue = TaskUtilities.CastTask(
                    Go(async() => {
                    await executorTask.ConfigureAwait(false);
                    var ex = responseDto.Exception as Exception;
                    if (ex != null)
                    {
                        throw ex;
                    }
                    return(responseDto.ReturnValue);
                }),
                    method.ReturnType);
                return;
            }

            try {
                executorTask.Wait();
            } catch (AggregateException ae) {
                if (ae.InnerExceptions.Count == 1)
                {
                    ExceptionDispatchInfo.Capture(ae.InnerExceptions[0]).Throw();
                    return; // unreachable
                }
                else
                {
                    throw;
                }
            }

            invocation.ReturnValue = responseDto.ReturnValue;

            var parameters = method.GetParameters();
            var outValues  = responseDto.Outs;

            for (int i = 0, outIndex = 0; i < parameters.Length && outIndex < outValues.Length; i++)
            {
                if (parameters[i].IsOut)
                {
                    invocation.Arguments[i] = outValues[outIndex++];
                }
            }
            var exception = responseDto.Exception as Exception;

            if (exception != null)
            {
                throw exception;
            }
        }