Compartir a través de


Walkthrough: Debugging a Managed Code Event Handler

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

You can write an event handler in managed code that is called before or after an X++ class method or when a delegate fires. This topic shows you how to debug the managed code post-event handler you created by following the steps in Walkthrough: Creating an Event Handler in Visual Studio.

Prerequisites

To complete this walkthrough you will need:

Debugging the Event Handler

The following procedures explain how to debug a managed code post-event handler that is called from X++. In this scenario, the event handler was created in C# and is called after the X++ class method MyClass.myMethod fires. You can debug the event handler in one of two ways:

  • From X++ - You debug by starting in X++ and then switching into the Visual Studio debugger. First, you manually attach to the Microsoft Dynamics AX process from Visual Studio and then switch to X++ to begin debugging. Because the event handler is managed code, you set the breakpoints and debug in Visual Studio; however, context automatically switches from the Microsoft Dynamics AX Debugger to the Visual Studio Debugger.

  • From Visual Studio - You debug the complete scenario from Visual Studio. First, you set the Debug Target and Startup Element properties on the project in Visual Studio. Then when you start the project, Visual Studio will attach to the correct process, run the code you specify in the Startup Element property, and then stop at the breakpoints in the managed code.

Warning

To step through these procedures, you must have the code artifacts deployed from topic Walkthrough: Creating an Event Handler in Visual Studio.

To debug the event handler from X++

  1. Open Microsoft Dynamics AX and navigate to the Jobs node in the AOT.

  2. Start Visual Studio and open the project MyEventHandler.

  3. Open the MyPostEventHandler class and set a breakpoint on the following line of code:

    Global.info("MyPostEventHandler.PostMyMethod called");
  1. Select Debug > Attach to Process.

  2. From the Available Processes field, select the process Ax32.exe and then click Attach. This is the Microsoft Dynamics AX client process. You must attach to the client process because the code that is being tested runs on the client.

  3. Switch to the Microsoft Dynamics AX client. Right-click the callMyClass job and select Open. The X++ code runs and then context switches to Visual Studio with the cursor positioned on the breakpoint that you previously set.

To debug the event handler from Visual Studio

  1. Start Visual Studio and open the project MyEventHandler.

  2. Open the MyPostEventHandler class and set a breakpoint on the following line of code:

    Global.info("MyPostEventHandler.PostMyMethod called");
  1. In Solution Explorer, click the project MyEventHandler. In the Properties Pane, set the Debug Target property to Client and set the Startup Element property to Jobs\callMyClass.

  2. Press F5 to start debugging. Visual Studio automatically attaches to the client process and calls the callMyClass job because you set the values of the Debug Target and Startup Element properties. After the debugger calls the callMyClass job, the method will fire, the post-event handler will be called, and code execution will stop at the breakpoint that you set in step 2.

Next Steps

This scenario shows you how to debug a post-event handler. You follow the same procedure to debug a pre-event handler. When debugging a pre-event handler, you may want to set a breakpoint in the X++ class method. This enables you to continue to debug after the pre-event handler completes and control is transferred to the X++ class method.

See also

Walkthrough: Creating an Event Handler in Visual Studio