![]() |
#1 |
Участник
|
sumitsaxfactor: Create Your First Custom Service [AX 2012]
Источник: http://sumitsaxfactor.wordpress.com/...rvice-ax-2012/
============== As AX has advanced, so has its integration with WCF. One of the good concepts that MS has introduced in AX 2012 is custom services. With custom services, anything and everything in AX can be exposed as Windows Communication Framework (WCF) service. This article concentrates on helping you create your first custom service. For creating and exposing custom service, following steps are involved:
Simple Scenario: The scenario we will take in this examples is customers. We will write a service to fetch all customers from default company. Add these customers to a list. Now select a subset of customers, pass it back to Ax and get their names. With this we can see how we can set and get an array of values. Create Custom Service Class Do the following:
/// Custom service class to demonstrate the creation and consumption of WCF services from AX /// /// /// This is the custom service class for fetching customer ids and customer names /// class SamCustomServiceTest { }
/// Gets a list of customer ids from the specified company /// /// /// List of customer Ids /// /// /// AifCollectionTypeAttribute is used to define strongly typed containers for data /// [AifCollectionTypeAttribute('return', Types::String), SysEntryPointAttribute(true)] public List retrieveCustomerIds() { List resultSet = new List(Types::String); CustTable custTable; whileselect custTable { resultSet.addEnd(custTable.AccountNum); } return resultSet; }
/// Gets a list of customer ids from the specified company /// /// /// The list of customer ids; Mandatory /// /// /// List of customer Ids /// /// /// AifCollectionTypeAttribute is used to define strongly typed containers for data /// [SysEntryPointAttribute(true), AifCollectionTypeAttribute('return', Types::String), AifCollectionTypeAttribute('_custList', Types::String)] public List retrieveCustomerNames(List _custList) { ListEnumerator listEnum = _custList.getEnumerator(); List resultSet = new List(Types::String); CustTable custTable; while (listEnum.moveNext()) { selectfirstOnly custTable where custTable.AccountNum == listEnum.current(); if (custTable) { resultSet.addEnd(custTable.AccountNum + “: “+ custTable.name()); } else { resultSet.addEnd(listEnum.current() + “: Customer not found”); } } return resultSet; } ![]() ![]() Create Custom Service
![]()
![]() Save the service and your service is ready now. ![]() Create and Deploy Service Group
![]() Verify the Deployment
![]() Consuming Custom Service in Visual Studio (C#) Now that the custom service is ready, let us go ahead and use that in a .Net Application. For that open your Visual Studio 2008 or 2010 development environment and do the following
![]()
![]()
![]() ![]()
{ SamCustomAXService.SamCustomServiceClient servClient = new SamCustomAXService.SamCustomServiceClient(); string[] custIds = servClient.retrieveCustomerIds(new SamCustomAXService.CallContext()); listBox1.Items.Clear(); foreach (string custId in custIds) listBox1.Items.Add(custId); }
{ SamCustomAXService.SamCustomServiceClient servClient = new SamCustomAXService.SamCustomServiceClient(); string[] strItem = null; int i = 0; strItem = newstring[listBox1.SelectedItems.Count]; //Get selected customer ids and prepare a string array foreach (Object selecteditem in listBox1.SelectedItems) { string item = selecteditem asstring; strItem[i++] = item; } //Use the string array to get the “Customer Id: Customer Name” Combo data string[] custIds = servClient.retrieveCustomerNames(new SamCustomAXService.CallContext(), strItem); listBox2.Items.Clear(); foreach (string custId in custIds) listBox2.Items.Add(custId); }
![]() This concludes the session on creating custom services. To conclude, this is something really cool that MS has introduced in AX. Happy servicing the AX guys ![]() ![]() Источник: http://sumitsaxfactor.wordpress.com/...rvice-ax-2012/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|