2014-02-12 43 views
0

我想通過使用MVC WebAPI的Dynamics AX 2012的查詢服務進行分頁來獲取記錄。 當我在控制檯應用程序中運行此代碼工作正常。查詢服務PositionBasedPaging在MVC中無效WebAPI

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); 

但當MVC的WebAPI的運行提供了錯誤

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: 

任何幫助相同的代碼?我浪費了我的時間來解決這個問題,但沒有成功......

+0

如果你擺脫尋呼會發生什麼?你有什麼結果嗎?也許開始位置超出範圍,因爲沒有結果返回? –

回答

1

問題解決了! 在WebAPI中使用時,Dynamics AX 2012的QueryService需要額外的屬性來爲給定的任何屬性提供值true。 這些屬性是後綴名稱爲「指定」。這些屬性必須賦值爲真,否則將拋出異常或不返回數據。

正確的代碼如下:

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);