Friday, December 19, 2008

Comunicate Ax 4.0 and .Net Application using BusinessConnector

If you try to connect an application from .NET with Axapta 4.0, you can do it using the BusinessConnector, which come with the Ax Client.

Supose you have the following method on an Ax Class called TestClass:

int SumNet(int a, int b)
{
;
return a + b;

}

To execute the method SumNet from a .NET Application you need to do the following:

1. Create a proyect on Visual Studio .NET (2005 or 2008)

2. On the proyect add reference, and on the dialog, choose browse. Locate the folder where the Ax client is installed, and in the folder Bin, select and add the reference to the Microsoft.Dynamics.BusinessConnectorNet.dll

3. Use the following code from the .NET Proyect to call the method:

//Create the Axapta object to connect with Ax
Microsoft.Dynamics.BusinessConnectorNet.Axapta ax = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();

//Create the object to represent the TestClass from Ax on .NET
Microsoft.Dynamics.BusinessConnectorNet.AxaptaObject axObj;

//Logon on Axapta
ax.Logon(null, null, null, null);


//Create an instance for the class
axObj = ax.CreateAxaptaObject("TestClass");

//Create object to be the parameters used in the call to the class on Axapta.
object[] obj = new object[2];
obj[0] = 5;
obj[1] = 13;

//Call the method SumNet on Ax for the class TestClass
int nResult = (int)axObj.Call("SumNet", obj);

//Show the result
MessageBox.Show(nResult.ToString());

//Don't forget do the LogOff on Axapta
ax.Logoff();


In this example, we supose that the Ax Client is configured to the correct AOS and the user has permission to use Axapta, in other case, in the method LogOn you need to specify other parameters to make the correct logon.



That's all, it's easy.

No comments: