예제 #1
0
        public override async Task ProcessAsync(IOwinContext context, CancellationToken cancellationToken)
        {
            IActionRuntimeProvider provider =
                Application.CurrentApplication.Options.Bootstrapper.RuntimeManager.FindRuntimeByName(_resolveResult.ControllerName);

            if (provider != null)
            {
                ISessionProvider sessionProvider = DefaultSessionProvider.DefaultProvider;

                ISession session = await sessionProvider.AccessAsync(context);

                if (!context.Items.ContainsKey("session"))
                {
                    context.Items.Add("session", session);
                }
                else
                {
                    context.Items["session"] = session;
                }

                object actionResult = provider.ExecuteAsync(_resolveResult.ControllerName, _resolveResult.ActionName, context);
                if (actionResult != null)
                {
                    if (actionResult is Task && actionResult.GetType().GetTypeInfo().IsGenericType) //Task<TResult>
                    {
                        //get task's result
                        var taskResultProperty = actionResult.GetType().GetTypeInfo().GetProperty("Result");
                        actionResult = taskResultProperty.GetValue(actionResult);
                    }

                    if (actionResult is IActionResult)
                    {
                        await((IActionResult)actionResult).ProcessAsync(context);
                    }
                    else
                    {
                        var responseProcessor = ResponseProcessorFactory.GetResponseProcessor(context);
                        if (responseProcessor != null)
                        {
                            await responseProcessor.ProcessAsync(actionResult);
                        }
                        else
                        {
                            throw new NullReferenceException(string.Format("Can't found the response processor process the request! The ContentType ={0}", context.Request.Headers.ContentType));
                        }
                    }
                }
            }
            else
            {
                throw new NullReferenceException("Not Action Runtime Provider!");
            }
        }