コード例 #1
0
ファイル: Program.cs プロジェクト: yuzuruqyhe/WebAPI
        static void Main(string[] args)
        {
            Controller controller   = new Controller();
            MethodInfo actionMethod = typeof(Controller).GetMethod("Get");

            CancellationToken cancellationToken = new CancellationToken();

            List <IActionFilter> filters = new List <IActionFilter>()
            {
                new FirstActionFilter(),
                new SecondActionFilter()
            };
            ActionContext context = new ActionContext()
            {
                Controller = controller,
                Action     = actionMethod,
                Arguments  = new object[0]
            };
            ActionInvoker invoker = new ActionInvoker(context.Action);

            Func <Task <ResponseMessage> > result = () =>
            {
                return(invoker.Invoke(context, cancellationToken));
            };

            for (int i = 0; i <= filters.Count - 1; i++)
            {
                IActionFilter filter = filters[i];

                Func <Func <Task <ResponseMessage> >, IActionFilter, Func <Task <ResponseMessage> > > chainContinuation =
                    (continuation, innerFilter) =>
                {
                    return(() =>
                    {
                        return innerFilter.ExecuteActionFilterAsync(context, cancellationToken, continuation);
                    });
                };
                result = chainContinuation(result, filter);
            }
            //string res = continuation()().Result.Message;
            result.Invoke();
            Console.ReadKey();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: BarlowDu/WebAPI
        static void Main(string[] args)
        {
            Controller controller = new Controller();
            MethodInfo actionMethod = typeof(Controller).GetMethod("Get");

            CancellationToken cancellationToken = new CancellationToken();

            List<IActionFilter> filters = new List<IActionFilter>() {
            new FirstActionFilter(),
            new SecondActionFilter()
            };
            ActionContext context = new ActionContext()
            {
                Controller = controller,
                Action = actionMethod,
                Arguments = new object[0]
            };
            ActionInvoker invoker = new ActionInvoker(context.Action);

            Func<Task<ResponseMessage>> result = () =>
            {
                return invoker.Invoke(context, cancellationToken);
            };

            for (int i = 0; i <= filters.Count - 1; i++)
            {
                IActionFilter filter = filters[i];

                Func<Func<Task<ResponseMessage>>, IActionFilter, Func<Task<ResponseMessage>>> chainContinuation =
                    (continuation, innerFilter) =>
                    {
                        return () =>
                        {
                            return innerFilter.ExecuteActionFilterAsync(context, cancellationToken, continuation);
                        };
                    };
                result = chainContinuation(result, filter);
            }
            //string res = continuation()().Result.Message;
            result.Invoke();
            Console.ReadKey();
        }