Today I decide to experiment with custom service. So I decided to create custom table with Name MyColorTable.
This table contains only one field Name. and made it unique with no allow duplicate on Index.
If we consider custom service in Dynamics Ax 2012, it contains following objects
- X++ Data contract class
- X++ Service contract class
- Service Node
- Service Group node.
Data Contract class
So first we create Data contract class
Suppose Our data contract class name is ColorDc
[DataContractAttribute] class ColorDC { Name ColorName; }
Now add a new method with Name ParmColorName and set its as
[ DataMemberAttribute('ColorName')] public Name parmColorName(Name _ColorName=ColorName) { ColorName=_ColorName; return ColorName; }
Service contract class:
Now we write a Service class which contains Three method Purpose of these method to explore the require attribute for getting parameter in service method and return list from service method.
InsertColor. (This method contains single color)
InsertColorList (This method take list of colorDC as parameter).
GetColorList ( This method return list of colorDC).
[SysEntryPointAttribute(true), AifCollectionTypeAttribute('Colorobj', Types::Class)] public void InsertColor(ColorDC Colorobj) { MyColorTable _Color; Name _Name; _Name = Colorobj.parmColorName(); select * from _Color where _Color.Name== _Name; if (_Color==null) { try { ttsBegin; _Color.Name = _Name; _Color.insert(); ttsCommit; } catch { ttsAbort; } } }
In above method we add some attribute which allow this method to act as web method and attribute help us define the expected parameter for this method call.
Now we call create another method, here attributes also described method as webmethod and also method expect what type of parameter.
[DataMemberAttribute("InsertColorList"), AifCollectionTypeAttribute("ColorList",Types::Class, classStr(ColorDC)) ] public Void InsertColorList(List ColorList ) { ListIterator iterator; ListEnumerator enumerator; ListIterator literator; ColorDC _color; enumerator = ColorList.getEnumerator(); while(enumerator.moveNext()) { _color= enumerator.current(); if (_color !=null) { this.InsertColor(_color); } } }
Now get method which return all color in Ax.
[SysEntryPointAttribute(true), AifCollectionTypeAttribute('return', Types::Class, classStr(ColorDC))] public list GetColorList() { ColorDC Colorobj; List _ColorList = new List(Types::Class); MyColorTable Colorbuf; while select * from Colorbuf { Colorobj = new ColorDC(); Colorobj.parmColorName(Colorbuf.Name); _ColorList.addEnd(Colorobj); } return _ColorList; }
Now compile it now
Service Object:
create a service object and set Service contract class there
Expand ColorService object and Right click on Operations and click on add method, this way we can restrict which method need to expose in service or which method need not.
Click on all check boxes and enabled them
Service Group:
Create a new service group and drag and drop service object under it.
Right click on ColorServiceGroup and deploy the service
Wait and let It deploy Info box shows the message that service is deployed successfully
No service is successfully deployed, Now open Ax client , from Administration section. Setup=> AIF => Inbond port.
Copy WSDL URI.
Open Visual studio and create a new Console application for testing the code.
Right click on references and ad service reference
First we call insert the records in service.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { ColorServiceGroup.ColorDC cd1 = new ColorServiceGroup.ColorDC(); cd1.ColorName = "Red"; ColorServiceGroup.ColorDC cd2 = new ColorServiceGroup.ColorDC(); cd2.ColorName = "Blue"; ColorServiceGroup.ColorDC cd3 = new ColorServiceGroup.ColorDC(); cd3.ColorName = "Yellow"; ColorServiceGroup.ColorDC[] DcList = new ColorServiceGroup.ColorDC[] { cd1, cd2, cd3 }; ColorServiceGroup.ColorServiceClient _Client = new ColorServiceGroup.ColorServiceClient(); ColorServiceGroup.CallContext _CallContext = new ColorServiceGroup.CallContext(); _CallContext.Company = "USMF"; _Client.InsertColorList(_CallContext, DcList); } } }
When I run the above code in Visual studio records are successfully inserted in original table.
Now we call list of colors exist in Dynamics Ax 2012.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { ColorServiceGroup.ColorServiceClient _Client = new ColorServiceGroup.ColorServiceClient(); ColorServiceGroup.CallContext _CallContext = new ColorServiceGroup.CallContext(); _CallContext.Company = "USMF"; ColorServiceGroup.ColorDC[] _DcList= _Client.GetColorList(_CallContext); // ColorServiceGroup.ColorDC _Dc ; foreach (ColorServiceGroup.ColorDC _Dc in _DcList) { Console.WriteLine(_Dc.ColorName); } Console.ReadKey(); } } }
When I run the above code, all records exists in Ax display on c# console
No comments:
Post a Comment