05.06.2013, 17:11 | #1 |
Участник
|
equalized: Dynamics Ax custom WCF service with paging support
Источник: http://www.kevinroos.be/2013/06/dyna...aging-support/
============== Hi all, Lately I’ve been busy developing WCF services to communicate with .NET web applications. All of these web services are custom-made and are using .NET data contracts so that every application uses the same contracts. Due to the high amount of data and performance we had to implement some kind of paging. I had no clue that Ax even has paging support but it does and it does this with properties on the QueryRun objects. For example purposes I’ve made a service which uses .NET request and response contracts. I prefer this way over X++ data contracts because this is more reusable and flexible on the client side. The code is self-explanatory to me but you can always pose questions of course. The request contract: 123456789[DataContract]public class ItemListRequest{ [DataMember] public long StartingPosition { get; set; } [DataMember] public long NumberOfRecordsToFetch { get; set; }} The response contract: 1234567891011121314151617181920[DataContract][KnownType(typeof(Item))]public class ItemListResponse{ [DataMember] public int TotalNumberOfRecords { get; set; } [DataMember] public ArrayList Items { get; set; }} [DataContract]public class Item{ [DataMember] public string Id { get; set; } [DataMember] public string Name { get; set; }} The service implementation: 12345678910111213141516171819202122232425262728293031323334353637[SysEntryPointAttribute]public Blog.WCFPaging.DataContracts.ItemListResponse getItems(Blog.WCFPaging.DataContracts.ItemListRequest _request){ Blog.WCFPaging.DataContracts.Item item; System.Collections.ArrayList itemList = new System.Collections.ArrayList(); Blog.WCFPaging.DataContracts.ItemListResponse response = new Blog.WCFPaging.DataContracts.ItemListResponse(); QueryRun queryRun = new QueryRun(queryStr(InventTable)); InventTable inventTable; ; if( CLRInterop::getAnyTypeForObject(_request.get_StartingPosition()) > 0 && CLRInterop::getAnyTypeForObject(_request.get_NumberOfRecordsToFetch()) > 0) { response.set_TotalNumberOfRecords(QueryRun::getQueryRowCount(queryRun.query(), maxInt())); queryRun.enablePositionPaging(true); queryRun.addPageRange(_request.get_StartingPosition(), _request.get_NumberOfRecordsToFetch()); // At least one order by field should be declared when using paging SysQuery::findOrCreateDataSource(queryRun.query(), tableNum(InventTable)).addOrderByField(fieldNum(InventTable, ItemId)); } while(queryRun.next()) { inventTable = queryRun.get(tableNum(InventTable)); item = new Blog.WCFPaging.DataContracts.Item(); item.set_Id(inventTable.ItemId); item.set_Name(inventTable.NameAlias); itemList.Add(item); } response.set_Items(itemList); return response;} Calling the service from a .NET application: 1234567891011121314151617181920212223242526int pageSize = 10; using (var client = new BLOGPagingServiceClient()){ BLOGPagingServiceGetItemsResponse response = null; var request = new ItemListRequest() { StartingPosition = 1, NumberOfRecordsToFetch = pageSize }; do { response = client.getItems(new BLOGPagingServiceGetItemsRequest() { CallContext = new CallContext(), _request = request }); foreach (Item item in response.response.Items) { Console.WriteLine(String.Format("{0, -10} - {1}", item.Id, item.Name)); } Console.WriteLine("-----"); request.StartingPosition += pageSize; } while (response.response.Items.Count > 0);} Paging on a QueryRun is implement since Ax 2009, more info on paging: http://msdn.microsoft.com/nl-be/libr...(v=ax.50).aspx I wonder why this isn’t implemented in the AIF services or is it? If anyone knows please leave a comment about it. Источник: http://www.kevinroos.be/2013/06/dyna...aging-support/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|