To increase the functionality of AX is possible to execute .NET code. In general terms the idea is create a .dll with the .NET code and after add the .dll as reference in AX. After that you’ll be able of execute the .NET code in AX.

Just follow these steps:

1. Create a new project (I’m going to use Visual Studio 2010):

VisualStudio2010_NewProject_thumb_5A3D55AC

VisualStudio_NewDLLProject_thumb_5A3D55AC

Please be sure that you’ve selected the .NET Framework 3.5: if you select a newest one AX won’t work with your .dll:

VS2010_NewProject_NETFramework35_thumb_5A3D55AC

2. Create the .NET code you want to execute:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyDLL { public class TestingMyDLL { public void showMessage() { MessageBox.Show(“Executing .NET code with AX!”); } } }

3. Build the solution:

VS2010_BuildSolutionDLL_thumb_082AA865

4. Locate the .dll generated under the Project folder:

VisualStudio2010_LocateDLLGenerated_thumb_082AA865

And copy it in your AX testing environment. Take care with the path in which you copy your .dll: it has to be located in the “bin” directory of your AX installation (in my case C:Program Files (x86)Microsoft Dynamics AX50ClientBin) otherwise AX will show you the following message:

DynamicsAX2009_AddingReferenceWarning_thumb_082AA865

And maybe you wont use the library:

DynamicsAX2009_UsingDLLError_thumb_082AA865

5. Import the dll into AX. Open the AOT and do right click in the References node and click ‘Add reference’:

DynamicsAX_ReferenceAdded_thumb_082AA865 DynamicsAX2009_AddingReferenceBrowse_thumb_082AA865 DynamicsAX2009_AOTAddReference_thumb_082AA865

 

 

Press OK.

If you selected the .NET Framework 4 when creating the project in step 1 you’ll get this error trying to import the reference:The selected file is not a valid .NET assembly, therefore the refence cannot be added.

VS2010_NETFramework_Error_thumb_1_082AA865

 

6. Create a new job/class in AX:

static void testingNETCode(Args _args)
{
    MyDLL.TestingMyDLL          testingDll;
    InteropPermission           permission;
    ;

    try
    {
        permission = new InteropPermission(InteropKind::DllInterop);
        permission.assert();

        testingDll = new MyDLL.TestingMyDLL();
        testingDll.showMessage();
    }
    catch
    {
        error('Error executing DLL code');
    }
}

7. Run the job:

DynamicsAX_ExecutingNETCode_Job_thumb_082AA865

 

Cheers!