// Only Used internally by TypedFilterTests
        public object Execute(object requestDto, IRequest req, bool applyFilters)
        {
            try
            {
                req.SetInProcessRequest();

                if (applyFilters)
                {
                    var task = AppHost.ApplyRequestConvertersAsync(req, requestDto);
                    task.Wait();
                    requestDto = task.Result;

                    AppHost.ApplyRequestFiltersAsync(req, req.Response, requestDto).Wait();
                    if (req.Response.IsClosed)
                    {
                        return(null);
                    }
                }

                var response = Execute(requestDto, req);

                return(applyFilters
                    ? ApplyResponseFilters(response, req)
                    : response);
            }
            finally
            {
                req.ReleaseIfInProcessRequest();
            }
        }
Esempio n. 2
0
        public override Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            try
            {
                appHost.AssertFeatures(format);

                if (appHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(TypeConstants.EmptyTask);
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString[Keywords.Callback];
                var doJsonp  = HostContext.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = httpReq.Dto = CreateRequest(httpReq, operationName);

                if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(TypeConstants.EmptyTask);
                }

                return(appHost.ApplyRequestFiltersAsync(httpReq, httpRes, request)
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        return t;
                    }

                    if (t.IsCanceled)
                    {
                        httpRes.StatusCode = (int)HttpStatusCode.PartialContent;
                        httpRes.EndRequest();
                    }

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    var rawResponse = GetResponse(httpReq, request);

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    return HandleResponse(rawResponse, response =>
                    {
                        UpdateResponseContentType(httpReq, response);
                        response = appHost.ApplyResponseConverters(httpReq, response);

                        if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                        {
                            return TypeConstants.EmptyTask;
                        }

                        if (doJsonp && !(response is CompressedResult))
                        {
                            return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                        }

                        return httpRes.WriteToResponse(httpReq, response);
                    });
                })
                       .Unwrap()
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        var taskEx = t.Exception.UnwrapIfSingleException();
                        return !HostContext.Config.WriteErrorsToResponse
                                ? taskEx.ApplyResponseConverters(httpReq).AsTaskException()
                                : HandleException(httpReq, httpRes, operationName, taskEx.ApplyResponseConverters(httpReq));
                    }
                    return t;
                })
                       .Unwrap());
            }
            catch (Exception ex)
            {
                return(!HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq)));
            }
        }