Friday, March 7, 2014

AX 2012 debug managed code executing on a remote AOS

A new feature in AX 2012 is that some pieces of code are run on the server side (AOS) and are executed in managed code.

For example X++ scheduled batch jobs are compiled to common intermediate language (CIL) before beeing executed on the AOS. This is the same for services code.

See the following link for more information :  X++ Compiled to .NET CIL

When this code is executed, it's sometimes pretty cool to be able to debug it.

Microsoft in his recommandations says that Visual Studio 2010 has to be installed on the AOS Server.
At first, I didn't see this recommandation and I tried to debug on my own computer.
It definitely seems to work if you use a UNC share for the source code to be available in Visual Studio.

Here are the step I followed :
  1. Install the visual studio remote debugging 
  2. Run the remote debugger
  3. Locate the sources
  4. Launch VS 2010 on your computer
  5. Open a Method
  6. Configure the solution properties
  7. Attach to the server

1. Install the remote debugging

- Download the Visual studio 2010 remote debuggin tool
- install it on the AOS. (I've chosen to install it as a service but you do as you wish.)

Also be sure to check the following options on the server configuration utility :



2. Run the remote debugger

You should have the following service started  :


3. Locates the sources

- Once the complete CIL is done, X++ source files are generated. These files are needed for visual studio to show us the code. It should be placed in the following folder :

C:\Program Files\Microsoft Dynamics AX\60\Server\<AOS_INSTANCE_FOLDER>\bin\XppIL\source

You should see thousands of .xpp files

Check you can access this folder with a share. For example : \\ServerName\C$\Program Files .....
or create a share yourself

4. Launch Visual studio 2010

- Be sure that you selected the right configuration in your Client configuration utility, or use the switch to the command line to choose the correct AOS.
- Launch the Dynamics AX Application Explorer to browse the AOT

5. Open a method

- When you double click on a method, a new solution will automatically be created.

6. Configure the solution properties

- Right click on the solution and click on the properties menu.
- Select debug source file in the list
- add the path to the source folder (the share you created before).



7. Attach visual studio to the AOS service


You can remotely debug the AOS without any problems.



Debugging Services in AX 2012


Regardless of the adapter type you choose, services in AX 2012 run as IL code - even if you are using the file adapter.  To debug IL code
you must use Visual Studio.
The steps for debugging a service are:
  1. On the AOS, install the AX Application Explorer from the Microsoft Dynamics AX CD by choosing the
    component Developer Tools, and then click Visual Studio Tools.  If the AX debugger is not installed,
    you will also need to install it.
  2. Open Visual Studio, create a new project, and in the AX Application Explorer find the methods in the
    service classes that are of interest.
  3. In Visual Studio add breakpoints where needed.
  4. Click Debug, and then click Attach to Process.
  5. Mark the checkboxes for Show processes from all users and Show processes in all sessions.
  6. Select AX32Serv.exe and click Attach.The Ax32Serv.exe won't be able available if you are not debugging on the AOS, so
    you need to be on the AOS.
  7. In a separate instance of VS (so have two different projects/solutions open) run the code that calls
    the service.One instance to run the code that
    calls the service and another instance of VS to do the debugging.  By
    having two instances open, you can leave the debugging instance attached to the
    AOS while you run the service code over and over again.

AX 2012 - How to debug Code in Visual Studio

This post basically addresses how to debug code in Visual studio.
Most of code running on AOS runs in CIL environment, so it cannot be debugg in Microsoft Dynamics AX Debugger.
You might have confused/surprised why ax debugger is not opening inspite of  inserting breakpoint at particular place. It means code might be running in CIL and to debug visual studio 2010 is required.

Step1: 
Make sure AOS Service runs on account which you logged in. Right click on AOS Service and change log on property to logged in windows credentials.
Log off windows and then re log in.          

Step2:
Open visual studio and click on AX Application explorer (Shortcut Ctrl + D)
AOT opens and you can browse nodes as you used to do in Dynamics AX.
Open the object which you want to debug and insert breakpoint.

Step3:
Then in Visual studio, Click on Debug->Attach process

As shown in above image, First make sure two check boxes at bottom are marked.
Then only you can find and able to select AX32Serv.exe(AOS)  process and click on attach button.

Note : When you are doing this step 3 for first time it prompts you to Restart Visual studio, kindly select the same option.

Step4:
You done with setting up visual studio to debug code running in CIL.
Now restrart process in Dynamics AX which you want to debugg and you can find Visual studio debugger opened for you.
Continue debugging with hotkeys as you use to do in AX debugger like F10/F11.


Happy Debugging :)

1 comment: