コード例 #1
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
        private async Task<ResponseMessage> ExecuteActionFilterAsyncCore(ActionContext actionContext, CancellationToken cancellationToken, Func<Task<ResponseMessage>> continuation)
        {
            await OnActionExecutingAsync(actionContext);

            if (actionContext.Response != null)
            {
                return actionContext.Response;
            }

            return await CallOnActionExecutedAsync(actionContext, cancellationToken, continuation);
        }
コード例 #2
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
 private async Task<ResponseMessage> CallOnActionExecutedAsync(ActionContext actionContext, CancellationToken cancellationToken, Func<Task<ResponseMessage>> continuation)
 {
     ResponseMessage response = null; ;
     try
     {
         response = await continuation();
     }
     catch (Exception ex)
     {
     }
     await OnActionExecutedAsync(actionContext, cancellationToken);
     return response;
     //throw new NotImplementedException();
 }
コード例 #3
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();
        }
コード例 #4
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
        public Task<ResponseMessage> ExecuteActionFilterAsync(ActionContext actionContext, System.Threading.CancellationToken cancellationToken, Func<Task<ResponseMessage>> continuation)
        {
            return ExecuteActionFilterAsyncCore(actionContext, cancellationToken, continuation);

        }
コード例 #5
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
 public Task OnActionExecutedAsync(ActionContext actionContext, CancellationToken cancellationToken)
 {
     OnActionExecuted(actionContext);
     return Task.FromResult<AsyncVoid>(default(AsyncVoid));
 }
コード例 #6
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
        public Task OnActionExecutingAsync(ActionContext actionContext)
        {
            OnActionExecuting(actionContext);

            return Task.FromResult<AsyncVoid>(default(AsyncVoid));
        }
コード例 #7
0
ファイル: ActionFilter.cs プロジェクト: BarlowDu/WebAPI
 public virtual void OnActionExecuted(ActionContext actionContext)
 {
 }
コード例 #8
0
ファイル: FirstActionFilter.cs プロジェクト: BarlowDu/WebAPI
 public override void OnActionExecuting(ActionContext actionContext)
 {
     Console.WriteLine("FirstActionFilter Executing");
 }
コード例 #9
0
ファイル: ActionInvoker.cs プロジェクト: BarlowDu/WebAPI
 public async Task<ResponseMessage> Invoke(ActionContext context, CancellationToken cancellationToken)
 {
     return await executor(context.Controller, context.Arguments);
 }
コード例 #10
0
ファイル: SecondActionFilter.cs プロジェクト: BarlowDu/WebAPI
 public override void OnActionExecuted(ActionContext actionContext)
 {
     Console.WriteLine("SecondActionFilter Executed");
 }