Ejemplo n.º 1
0
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            _stopWatch.Start();
            IMethodReturn result = getNext()(input, getNext);

            if (IgnoreMethod(input))
            {
                return(result);
            }

            var method = input.MethodBase as MethodInfo;

            if (result.ReturnValue != null &&
                method != null &&
                typeof(Task).IsAssignableFrom(method.ReturnType))
            {
                // If this method returns a Task, override the original return value.
                // More info on this pattern: https://msdn.microsoft.com/en-us/magazine/dn574805.aspx
                var task = (Task)result.ReturnValue;
                return(input.CreateMethodReturn(
                           GetWrapperCreator(method.ReturnType)(task, input), result.Outputs));
            }

            // If this method does not return a task, just log and return value.
            LogMethodResult(input, result.ReturnValue, result.Exception);
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Implementation of the behavioral processing.
        /// It logs the entry to this method, call the next on the interceptors pipeline, logs the return of that invocation or the exception if an error occurs and finally return to the caller the result received.
        /// Also this method takes care of async operations if is needed.
        /// </summary>
        /// <param name="input">Inputs to the current call to the target.</param>
        /// <param name="getNext">Delegate to execute to get the next delegate in the behavior chain.</param>
        /// <returns>Return value from the target</returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            //Log the call before the invocation.
            Trace(string.Format("{0}. Invoking method {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), input.MethodBase), TraceEventType.Information);

            //Invoke the next behavior in the chain (if this is the last interceptor in the pipeline then it invokes the target method).
            IMethodReturn result = getNext()(input, getNext);

            //After invoking the method on the original target log the return or the exception if an error occurs

            //Check if this method returns a Task
            MethodInfo method = input.MethodBase as MethodInfo;

            if ((result.ReturnValue != null) && (method != null) && (typeof(Task).IsAssignableFrom(method.ReturnType)))
            {
                //If this method returns a Task then override the original return value and handle the async operation
                Task task = (Task)result.ReturnValue;
                return(input.CreateMethodReturn(GetWrapperCreator(method.ReturnType)(task, input), result.Outputs));
            }
            else if (result.Exception != null)
            {
                //If this method has a single return and an exception is thrown then log the exception
                TraceError(input, result.Exception);
            }
            else
            {
                //If this method has a single return then just log it
                Trace(string.Format("{0}. Method {1} returned {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), input.MethodBase, result.ReturnValue), TraceEventType.Information);
            }

            //Return the result to the caller
            return(result);
        }
Ejemplo n.º 3
0
        //public void Intercept(IInvocation invocation)
        //{
        //    Console.WriteLine("TaskWrapperInterceptor call start");
        //    invocation.Proceed();

        //    // TODO: check how works with void.
        //    var returnType = invocation.Method.ReturnType;

        //    // Process task result.
        //    // TODO: check it.
        //    if (returnType == typeof (Task)) {
        //        var task = (Task) invocation.ReturnValue;
        //        invocation.ReturnValue = task.ContinueWith(
        //            continuation => Console.WriteLine("TaskWrapperInterceptor call end"));

        //        //invocation.ReturnValue = task.ConfigureAwait(false);
        //        // Process task with value.
        //    } else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task<>)) {
        //        Console.WriteLine(returnType.GetGenericTypeDefinition());
        //        Console.WriteLine(returnType);
        //        Console.WriteLine(invocation.ReturnValue);
        //        //var task2 = (Task<object>)invocation.ReturnValue;
        //        var task2 = (Task)invocation.ReturnValue;

        //        //MethodInfo method = returnType.GetMethod("ContinueWith");
        //        //MethodInfo genericMethod = method.MakeGenericMethod(returnType);

        //        //genericMethod.Invoke(task2, new object [];);

        //        //var t = returnType.GetGenericTypeDefinition();
        //        //var task2 = Transform<Task<>>(invocation.ReturnValue);

        //        var continueTask2 = task2.ContinueWith((Task continuation) => {
        //            Console.WriteLine("TaskWrapperInterceptor call end");
        //            var result = continuation.GetType()
        //                .GetProperty("Result")
        //                .GetValue(continuation, null);

        //            invocation.ReturnValue = result;
        //            //return continuation;
        //        });
        //        //invocation.ReturnValue = continueTask2;
        //    } else {
        //        Console.WriteLine("TaskWrapperInterceptor call end");
        //    }
        //}

        //private T2 Transform<T2>(object input) where T2: class
        //{
        //    return input as T2;
        //}

        public void Intercept(IInvocation invocation)
        {
            MethodStart();

            invocation.Proceed();

            // Process task result.
            if (invocation.ReturnValue is Task)
            {
                var task = (Task)invocation.ReturnValue;
                // Get wrapper creator for return type and returns wrapper task.
                invocation.ReturnValue = GetWrapperCreator(invocation.Method.ReturnType)(task, invocation);
            }
            else
            {
                MethodEnd();
            }
        }