使用GetAll(AsyncCrudAppService)并应用排序时出错

时间:2018-11-16 13:35:46

标签: asp.net-core aspnetboilerplate

我已经基于AsyncCrudAppService类构建了一个应用程序服务,如下所示:

public class ServiceTemplateAppService : AsyncCrudAppService<ServiceTemplate, ServiceTemplateDto, int, PagedAndSortedResultRequestDto, CreateServiceTemplateDto, UpdateServiceTemplateDto>, IServiceTemplateAppService

一切正常,但是偶尔我在调用GetAll方法并传递排序参数时遇到错误,如下所示:

api/services/app/CostCenter/GetAll?SkipCount=0&MaxResultCount=10&Sorting=displayName%20asc

我已经重写了CreateFilteredQuery方法,现在它看起来像这样,因为我想获得一些子实体。

protected override IQueryable<ServiceTemplate> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
     return Repository.GetAll()
         .Include(x => x.ServiceTemplateRole)
             .ThenInclude(y => y.Role)
         .Include(x => x.ServiceTemplateImage)
         .Include(x => x.ServiceTemplateCategory);
 }

我得到的错误消息是这样的:

INFO  2018-11-16 13:45:02,101 [21   ] ore.Mvc.Internal.ControllerActionInvoker - Route matched with {area = "app", action = "GetAll", controller = "ServiceTemplate"}. Executing action dsim.Services.ServiceTemplateAppService.GetAll (dsim.Application)
INFO  2018-11-16 13:45:02,104 [21   ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method dsim.Services.ServiceTemplateAppService.GetAll (dsim.Application) with arguments (Abp.Application.Services.Dto.PagedAndSortedResultRequestDto) - Validation state: Valid
ERROR 2018-11-16 13:45:02,189 [24   ] Mvc.ExceptionHandling.AbpExceptionFilter - Could not load type 'System.ComponentModel.DataAnnotations.BindableTypeAttribute' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
System.TypeLoadException: Could not load type 'System.ComponentModel.DataAnnotations.BindableTypeAttribute' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Linq.Dynamic.Core.CustomTypeProviders.AbstractDynamicLinqCustomTypeProvider.<>c.<FindTypesMarkedWithDynamicLinqTypeAttribute>b__0_1(Type x)
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
   at System.Linq.Dynamic.Core.CustomTypeProviders.DefaultDynamicLinqCustomTypeProvider.GetCustomTypes()
   at System.Linq.Dynamic.Core.Parser.KeywordsHelper..ctor(ParsingConfig config)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser..ctor(ParameterExpression[] parameters, String expression, Object[] values, ParsingConfig parsingConfig)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy(IQueryable source, ParsingConfig config, String ordering, Object[] args)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy[TSource](IQueryable`1 source, ParsingConfig config, String ordering, Object[] args)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy[TSource](IQueryable`1 source, String ordering, Object[] args)
   at Castle.Proxies.Invocations.CrudAppServiceBase`6_ApplySorting_3.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.ServiceTemplateAppServiceProxy.ApplySorting(IQueryable`1 query, PagedAndSortedResultRequestDto input)
   at Abp.Application.Services.AsyncCrudAppService`8.GetAll(TGetAllInput input)
   at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult[T](Task`1 actualReturnValue, Func`1 postAction, Action`1 finalAction)
   at lambda_method(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()

我试图找出可能导致问题的原因,并添加异常告诉我的程序集,但没有任何进展。奇怪的是,这有时会起作用,所以我猜想这与数据库中的实际数据有关,但是,嘿?我不知道...

1 个答案:

答案 0 :(得分:0)

GetAll方法通过AsyncCrudAppService调用ApplySorting方法,您可以覆盖它,并根据需要进行排序。

    protected override IQueryable<YourEntity> ApplySorting(IQueryable<YourEntity> query, PagedAndSortedResultRequestDto input)
    {
        if (input.Sorting.IsNullOrEmpty())
        {
            input.Sorting = "yourColumn asc";
        }
        return base.ApplySorting(query, input);
    }

如果您要创建另一个GetAll方法,请参见以下示例:

public Task<PagedResultDto<PaisDto>> GetAllFiltered(PagedAndSortedResultRequestDto input, string filter)
        {
            var paisList = new List<Pais>();
            var query = Repository.GetAll();

            query = ApplySorting(query, input);


            if (filter != null && filter != string.Empty)
            {
                paisList = query
                    .Where(x => x.Identificador.StartsWith(filter) || x.Nombre.StartsWith(filter))
                    .Skip(input.SkipCount)
                    .Take(input.MaxResultCount).ToList();

                var result = new PagedResultDto<PaisDto>(query.Count(), ObjectMapper.Map<List<PaisDto>>(paisList));
                return Task.FromResult(result);
            }
            else
            {

                paisList = query
                    .Skip(input.SkipCount)
                    .Take(input.MaxResultCount).ToList()
                    .ToList();

                var result = new PagedResultDto<PaisDto>(query.Count(), ObjectMapper.Map<List<PaisDto>>(paisList));
                return Task.FromResult(result);
            }
        }

query = ApplySorting(查询,输入); 在这一行中,您可以调用基本方法base.ApplySorting(query, input);