Monday, September 15, 2008

HOW TO: Connect to AX 4.0 from a .NET Application using Business connector

This article demonstrates how a we can connect to Microsoft Dynamics AX 4.0 from a .NET based application.

You can find a good explanation of Microsoft Dynamics AX from here.

For this you will need to have Microsoft Dynamics AX Client installed on your machine. You must also have Dynamics AX .NET Business connector installed on your machine - to connect to AX from .NET.

Once you have the client components installed, you must configure your client to connect to an Axapta server from Control panel -> Administrative tools ->Microsoft Dynamics AX Configuration Utility.




Once configured, you can open the AX client UI from Start-> Programs-> Microsoft Dynamics AX.



Lets now see how to invoke a simple method in AX from a .NET application.

The following method should be written in AX inorder to be invoked from .NET:
1. Navigate to AX UI
2. Click on Axapta Object Tree (AOT)
3. Expand classes node.
4. Add the following class and method:



5. Test this method by executing it as a Job.
(AOT-> Jobs ->Create a simple job -> add the above code with an Info statement.)

Once the above steps are executed successfully, the following are the steps for invoking this AX method in .NET.
1. Open a new Console application using Visual Studio 2008
2. Add the following Reference to the Project:
Microsoft.Dynamics.BusinessConnectorNet
This dll is present in:
C:\Program Files\Microsoft Dynamics AX\40\Client\Bin
3. Naviate to Program.cs file and add the following namespace for reference:
using Microsoft.Dynamics.BusinessConnectorNet;
4. Add the following code in main() to invoke the AX method:

static void Main(string[] args)
{
try
{
Axapta ax = new Axapta();
ax.Logon(null, null, null, null);
string strName = " Microsoft Dynamics AX ";
AxaptaObject axObject;
axObject = ax.CreateAxaptaObject("NetConnector");
string resonseFromAxapta = (string)axObject.Call("SampleMethod", strName);
Console.WriteLine(resonseFromAxapta);
Console.ReadLine();
ax.Logoff();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}


5. Compile and Run the Application.
Thats all about it! You now should see the data coming from Axapta in .NET.

No comments: