Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Query Service PositionBasedPaging doesn't work in MVC WebAPI.

Posted on by 15

When I run this code in Console Application works fine.

QueryServiceClient client = new QueryServiceClient();

            DataSet dataSet;
            Paging paging = new PositionBasedPaging() { StartingPosition = 1, NumberOfRecordsToFetch = 10 };

            QueryMetadata query = new QueryMetadata();
            QueryDataSourceMetadata customerDataSource;

            query.QueryType = QueryType.Join;
            query.DataSources = new QueryDataSourceMetadata[1];
            
            customerDataSource = new QueryDataSourceMetadata();
            customerDataSource.Name = "AccountNum";
            customerDataSource.Enabled = true;
            customerDataSource.FetchMode = FetchMode.OneToOne;
            customerDataSource.Table = "CustTable";
            customerDataSource.Company = "CEU";
            customerDataSource.DynamicFieldList = true;
            customerDataSource.OrderMode = OrderMode.OrderBy;
            
            query.DataSources[0] = customerDataSource;
            query.OrderByFields = new QueryDataOrderByMetadata[]{
                new QueryDataOrderByMetadata(){ DataSource="AccountNum", FieldName="AccountNum",SortOrder=SortOrder.Ascending}
            };

            dataSet = client.ExecuteQuery(query, ref paging);

but the same code when runs in MVC WebAPI gives error

System.ServiceModel.FaultException`1 was unhandled by user code
  HResult=-2146233087
  Message=Page size 'PositionBasedPaging.StartingPosition' is out of range.
Parameter name: PositionBasedPaging.StartingPosition
  Source=mscorlib
  Action=http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault
  StackTrace:
    Server stack trace:
       at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at SL_AX_MVC_WebAPI.QueryService.IQueryService.ExecuteQuery(ExecuteQueryRequest request)
       at SL_AX_MVC_WebAPI.QueryService.QueryServiceClient.SL_AX_MVC_WebAPI.QueryService.IQueryService.ExecuteQuery(ExecuteQueryRequest request) in d:\Projects\SL_AX_MVC\SL_AX_MVC_WebAPI\Service References\QueryService\Reference.cs:line 5269
       at SL_AX_MVC_WebAPI.QueryService.QueryServiceClient.ExecuteQuery(QueryMetadata queryMetadata, Paging& paging) in d:\Projects\SL_AX_MVC\SL_AX_MVC_WebAPI\Service References\QueryService\Reference.cs:line 5276
       at SL_AX_MVC_WebAPI.Repositories.CustomerRepository.getCustomers(Int32 skip, Int32 take) in d:\Projects\SL_AX_MVC\SL_AX_MVC_WebAPI\Repositories\CustomerRepository.cs:line 108
       at SL_AX_MVC_WebAPI.Repositories.CustomerRepository.Get(Int32 skip, Int32 take) in d:\Projects\SL_AX_MVC\SL_AX_MVC_WebAPI\Repositories\CustomerRepository.cs:line 47
       at SL_AX_MVC_WebAPI.Controllers.CustomerController.Get(Int32 skip, Int32 take) in d:\Projects\SL_AX_MVC\SL_AX_MVC_WebAPI\Controllers\CustomerController.cs:line 31
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
       at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
  InnerException:

Any help ?  i have wasted my time to tackle with this issue but no success...

*This post is locked for comments

  • Suggested answer
    Wajahat Profile Picture
    Wajahat 15 on at
    RE: Query Service PositionBasedPaging doesn't work in MVC WebAPI.

    Problem Solved!

    When using in WebAPI, QueryService of Dynamics AX 2012 needs additional properties to provide value true for whatever property is given.

    These properties are Post-fix with name "Specified". These properties must be given value true otherwise exception will be thrown or no data will return.

    Correct Code is below:

       QueryServiceClient client = new QueryServiceClient();

               DataSet dataSet;

               Paging paging = new PositionBasedPaging() { StartingPosition = 1, NumberOfRecordsToFetch = 10, NumberOfRecordsToFetchSpecified = true, StartingPositionSpecified = true };

               QueryMetadata query = new QueryMetadata();

               QueryDataSourceMetadata customerDataSource;

               query.QueryType = QueryType.Join;

               query.DataSources = new QueryDataSourceMetadata[1];

               customerDataSource = new QueryDataSourceMetadata();

               customerDataSource.Name = "AccountNum";

               customerDataSource.Enabled = true;

               customerDataSource.FetchMode = FetchMode.OneToOne;

               customerDataSource.Table = "CustTable";

               customerDataSource.Company = "CEU";

               customerDataSource.DynamicFieldList = true;

               customerDataSource.OrderMode = OrderMode.OrderBy;

               customerDataSource.EnabledSpecified =

               customerDataSource.FetchModeSpecified =

               customerDataSource.DynamicFieldListSpecified =

               customerDataSource.OrderModeSpecified = true;

               query.DataSources[0] = customerDataSource;

               query.OrderByFields = new QueryDataOrderByMetadata[]{

                   new QueryDataOrderByMetadata(){ DataSource="AccountNum", FieldName="AccountNum",SortOrder=SortOrder.Ascending}

               };

               dataSet = client.ExecuteQuery(query, ref paging);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans