13.06.2012, 22:11 | #1 |
Участник
|
sumitsaxfactor: Data Contracts and Custom Services [AX 2012]
Источник: http://sumitsaxfactor.wordpress.com/...vices-ax-2012/
============== In my previous post, I had explained the procedure to create custom services. The post contained usage of Basic types for exchanging data. In this post, I will explain the way to use complex data types and exchanging data where we need to provide or receive more than one data field as request/response. To accomplish this, Dynamics AX provides concept of Data Contracts. Data Contracts are used to specify the input and return parameters for Services. Microsoft Dynamics AX utilizes the WCF data contracts to use .Net and X++ data types (both basic and complex). The serialization and deserialization of Data Contracts is handled by WCF framework itself. What we as developers need to ensure is that we specify proper attributes to the contract classes and methods to ensure that proper serialization and deserialization happens for our contract classes. Serialization Attributes
Described below is the procedure to create and use the data contract classes. Note that you will need to first finish the task specified in my previous blog to create custom service to carry on with this procedure.
/// Data Contract class for SamCustomServiceTest custom service demo class /// /// /// This is the Data Contract class for the custom service demo class SamCustomServiceTest /// [DataContractAttribute] class SamCustomServiceContract { CustName custName; CustGroupId custGroupId; LanguageId defaultLanguage; }
/// /// Gets or sets the value of the datacontract parameter CustGroupId. /// /// /// The new value of the datacontract parameter CustGroupId; optional. /// /// /// The current value of datacontract parameter CustGroupId /// [ DataMemberAttribute('CustGroupId') ] public CustGroupId parmCustGroupId(CustGroupId _custGroupId = custGroupId) { custGroupId = _custGroupId; return custGroupId; } parmCustName /// /// Gets or sets the value of the datacontract parameter CustName. /// /// /// The new value of the datacontract parameter CustName; optional. /// /// /// The current value of datacontract parameter CustName /// [ DataMemberAttribute('CustName') ] public CustName parmCustName(CustName _custName = custName) { custName = _custName; return custName; } parmDefaultLangauge /// /// Gets or sets the value of the datacontract parameter DefaultLanguage. /// /// /// The new value of the datacontract parameter DefaultLanguage; optional. /// /// /// The current value of datacontract parameter DefaultLanguage /// [ DataMemberAttribute('DefaultLanguage') ] public LanguageId parmDefaultLanguage(LanguageId _defaultLanguage = defaultLanguage) { defaultLanguage = _defaultLanguage; return defaultLanguage; } The Contract Class is ready for utilization. Now let us go ahead and add a method to our custom service class to utilize this contract class. For this, select our previously created service class, SamCustomServiceTest and add following method to the class /// /// Gets a list of customer ids from the specified company /// /// /// The list of customer ids; Mandatory /// /// /// List of customer information /// /// /// AifCollectionTypeAttribute is used to define strongly typed containers for data /// [SysEntryPointAttribute(true), AifCollectionTypeAttribute('return', Types::Class, classStr(SamCustomServiceContract)), AifCollectionTypeAttribute('_custList', Types::String)] public List retrieveCustomerInfo(List _custList) { SamCustomServiceContract dataContract; ListEnumerator listEnum = _custList.getEnumerator(); List resultSet = new List(Types::Class); CustTable custTable; while (listEnum.moveNext()) { selectfirstOnly custTable where custTable.AccountNum == listEnum.current(); dataContract = new SamCustomServiceContract(); if (custTable) { dataContract.parmCustGroupId(custTable.CustGroup); dataContract.parmCustName(custTable.name()); dataContract.parmDefaultLanguage(custTable.languageId()); } resultSet.addEnd(dataContract); } return resultSet; } Note: The return type is class and we have to provide the name of the serializable class that will be used for returning data. The return type is specified by AifCollectionTypeAttribute attribute.
{ string[] strItem = null; int i = 0; SamCustomAXService.CallContext callContext = new SamCustomAXService.CallContext(); SamCustomAXService.SamCustomServiceClient servClient = new SamCustomAXService.SamCustomServiceClient(); 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 Data Contract array to get the customer information SamCustomAXService.SamCustomServiceContract[] custInfo = servClient.retrieveCustomerInfo(callContext, strItem); listView1.Items.Clear(); i = 0; foreach (SamCustomAXService.SamCustomServiceContract custDet in custInfo) { ListViewItem item = listView1.Items.Add(strItem[i++]); item.SubItems.Add(custDet.CustName); item.SubItems.Add(custDet.CustGroupId); item.SubItems.Add(custDet.DefaultLanguage); } } Now save the package, build it and run, first click on button “Get Customers”, then select some customers and click on button “Get Customer Info” this is what you will see This concludes the post, I will keep on adding more topics on custom services as and when I try them out. Источник: http://sumitsaxfactor.wordpress.com/...vices-ax-2012/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|