2018-04-25

[AX2009] AX COM Business Connector

This is how to create a connection from C# to Dynamics AX 2009.
Install AxCom.dll
  1. Run  D:\Setup.exe HideUI=1 AcceptLicenseTerms=1 InstallComBusinessConnector=1 if it is failed, find AxCom.dll from installation and copy the file into C:\Program Files (x86)\Microsoft Dynamics AX\50\Client\Bin.
  2. Register AxCom.dll by regSvr32 in command prompt.
Create a Class in AX
  1. Create axCom class.
  2. Create method getName.
Name getName(Name _name)
{
    ;
    return strfmt("-- %1 --",_name);
}
Create a C# Console program (Just a sample) 
  1. Add reference to AxCom.dll, in tab COM, select Axapta Com Connector 1.2 Type Library.
  2. C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxaptaCOMConnector;//AX Business Connector

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Axapta3             ax = new Axapta3();
            IAxaptaObject       axObj;
            string              axName;
            AxaptaParameterList list = new AxaptaParameterList();
            String              name;

            name = "Default Name";
            //axCOM.Logon("CEU", "", "", "");
            ax.LogonAs("axService", "DAX", "axProxy", "DAX", "Password");//log on as AxService user in DAX domain over on proxy user. Add parameters as needed.
            axObj = ax.CreateObject("axCom", null, null, null, null, null, null);//create object from axCom class in AX

            name = System.Console.ReadLine();//get name from console
          
            //create parameter
            list.Size=1;
            list.set_Element(1, name);
          
            axName = (string)axObj.CallEx("getName", list);//execute method in axCom class
          
            System.Console.WriteLine(axName);//show modified name in console
            System.Console.ReadKey();//wait until hit a key
          
            ax.Logoff();//log out
        }
    }
}