public override void OnActionExecuted(ActionExecutedContext context)
        {
            var qs = QueryFns.ExtractAndDecodeQueryString(context);

            if (qs == null)
            {
                base.OnActionExecuted(context);
                return;
            }

            var queryable = QueryFns.ExtractQueryable(context);

            if (queryable == null)
            {
                base.OnActionExecuted(context);

                return;
            }

            var eq      = new EntityQuery(qs);
            var eleType = TypeFns.GetElementType(queryable.GetType());

            eq.Validate(eleType);


            int?inlineCount = null;

            queryable = eq.ApplyWhere(queryable, eleType);

            if (eq.IsInlineCountEnabled)
            {
                inlineCount = (int)Queryable.Count((dynamic)queryable);
            }

            queryable = eq.ApplyOrderBy(queryable, eleType);
            queryable = eq.ApplySkip(queryable, eleType);
            queryable = eq.ApplyTake(queryable, eleType);
            queryable = eq.ApplySelect(queryable, eleType);
            queryable = eq.ApplyExpand(queryable, eleType);

            // if a select or expand was encountered we need to
            // execute the DbQueries here, so that any exceptions thrown can be properly returned.
            // if we wait to have the query executed within the serializer, some exceptions will not
            // serialize properly.
            var listResult   = Enumerable.ToList((dynamic)queryable);
            var qr           = new QueryResult(listResult, inlineCount);
            var breezeConfig = context.HttpContext.RequestServices.GetService <IBreezeConfig>();

            context.Result = new ObjectResult(qr)
            {
                Formatters = new FormatterCollection <IOutputFormatter>
                {
                    #if NETCOREAPP3_1 || NETCOREAPP3_0
                    new NewtonsoftJsonOutputFormatter(breezeConfig.GetJsonSerializerSettings(),
                                                      context.HttpContext.RequestServices.GetRequiredService <ArrayPool <char> >(),
                                                      context.HttpContext.RequestServices.GetRequiredService <MvcOptions>())
                    #else
                    new JsonOutputFormatter(breezeConfig.GetJsonSerializerSettings(),
                                            context.HttpContext.RequestServices.GetRequiredService <ArrayPool <char> >())
                    #endif
                }
            };

            var session = GetSession(queryable);
            if (session != null)
            {
                Close(session);
            }

            base.OnActionExecuted(context);
        }
Beispiel #2
0
        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            var qs = QueryFns.ExtractAndDecodeQueryString(context);

            if (qs == null)
            {
                base.OnActionExecuted(context);
                return;
            }

            var queryable = QueryFns.ExtractQueryable(context);

            if (queryable == null)
            {
                base.OnActionExecuted(context);
                return;
            }

            var eq      = new EntityQuery(qs);
            var eleType = TypeFns.GetElementType(queryable.GetType());

            eq.Validate(eleType);


            int?inlineCount = null;

            var originalQueryable = queryable;

            queryable = eq.ApplyWhere(queryable, eleType);

            IFutureValue <int> inlineCountFuture = null;

            if (eq.IsInlineCountEnabled)
            {
                if (UseFuture)
                {
                    var countExpr = GetCountExpressionMethod.MakeGenericMethod(eleType).Invoke(this, new object[0]);
                    inlineCountFuture = (IFutureValue <int>)ToFutureValueMethod.MakeGenericMethod(eleType, typeof(int))
                                        .Invoke(null, new[] { queryable, countExpr });
                }
                else
                {
                    inlineCount = (int)Queryable.Count((dynamic)queryable);
                }
            }

            queryable = eq.ApplyOrderBy(queryable, eleType);
            queryable = eq.ApplySkip(queryable, eleType);
            queryable = eq.ApplyTake(queryable, eleType);
            queryable = eq.ApplySelect(queryable, eleType);
            queryable = eq.ApplyExpand(queryable, eleType);


            if (queryable != originalQueryable)
            {
                // if a select or expand was encountered we need to
                // execute the DbQueries here, so that any exceptions thrown can be properly returned.
                // if we wait to have the query executed within the serializer, some exceptions will not
                // serialize properly.

                dynamic listResult;
                if (UseFuture)
                {
                    var future = ToFutureMethod.MakeGenericMethod(eleType).Invoke(null, new object[] { queryable });
                    listResult = Enumerable.ToList((dynamic)future.GetType().GetMethod("GetEnumerable").Invoke(future, new object[0]));
                }
                else
                {
                    listResult = Enumerable.ToList((dynamic)queryable);
                }

                var qr = new QueryResult(listResult, inlineCountFuture?.Value ?? inlineCount);

                if (CloseSession)
                {
                    var session = GetSession(queryable);
                    if (session != null)
                    {
                        Close(session);
                    }
                }

                context.Response = context.Request.CreateResponse(HttpStatusCode.OK, qr);
            }

            base.OnActionExecuted(context);
        }