我想通过使用Dynamics AX 2012的查询服务通过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。 这些属性是Post-fix,名称为“指定”。必须将这些属性赋值为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);