Chapter 3: Run and Debug Add-ins


 

MicroStation Add-ins are .NET assemblies that run inside the MicroStation process. Unlike native DLLs, a .NET assembly cannot be unloaded once it has been loaded into MicroStation.

This is an important difference between .NET Add-ins and native applications:

Because of this, whenever you make changes to your Add-in code and need to rebuild the project, you must first close MicroStation if it is already using the Add-in.

 

Two Ways to Debug an Add-in

There are two common ways to debug a MicroStation Add-in:

  1. Start MicroStation from Visual Studio
  2. Attach Visual Studio to an already running MicroStation process

The first method is recommended when developing and testing your Add-in.

 

Method 1: Start MicroStation from Visual Studio

Step 1: Configure the Project

Open your project in Visual Studio.

In the project properties:

  1. Select the Debug page.
  2. Set Start external program to the path of microstation.exe.

C:\Program Files\Bentley\MicroStation 2026\MicroStation\microstation.exe

  1. Enter a test DGN file in the Command ine arguments field.

D:\elements.dgn

Note: This configuration only needs to be completed once for each project.

The following image shows an example configuration.

 

Step 2: Set a Breakpoint

Open MyAddins.cs and locate the following method call:

CreateElement.LineAndLineString1 ();

Click in the left margin next to the line number to set a breakpoint.

When a breakpoint is set:

This tells Visual Studio to pause execution when the code reaches that location.

 

Step 3: Start Debugging

Click the Start Debugging button (the green arrow) on the Visual Studio toolbar.

Visual Studio will launch MicroStation.

Once MicroStation starts:

  1. Open the Key-in window.
  2. Enter:

  MDL LOAD csAddins2

  1. Press Enter.

When execution reaches the breakpoint, Visual Studio will automatically pause the program.

 

Step 4: Step Through the Code

The Visual Studio Debug toolbar provides several useful commands, including:

If the Debug toolbar is not visible, enable it from:

View > Toolbars > Debug

Click Step Into to enter the LineAndLineString1() method in CreateElement.cs.

 

Step 5: Inspect Variables

While debugging, move the mouse pointer over a variable.

Visual Studio will display:

This feature is useful for understanding program behaviour and verifying that values are correct.

 

Step 6: Stop Debugging

To end the debugging session:

  1. Click Stop Debugging on the Debug toolbar.
  2. Visual Studio will stop debugging.
  3. The MicroStation instance started by Visual Studio will automatically close.

Visual Studio provides many additional debugging tools, such as Watches, Call Stack, Immediate Window, and Conditional Breakpoints. These advanced topics are outside the scope of this tutorial but are worth exploring as you become more familiar with Visual Studio.

 

 

Method 2: Attach to an Existing MicroStation Process

This method is useful when MicroStation is already running and you want to debug your Add-in without launching a new session from Visual Studio.

Unlike Method 1, no special Debug configuration is required.

Step 1: Start MicroStation

Launch MicroStation normally.

 

Step 2: Set a Breakpoint

In Visual Studio, set a breakpoint in:

CreateElement.LineAndLineString1 ();

 

For detailed instructions, refer to Step 2 of Method 1.

 

Step 3: Attach Visual Studio to MicroStation

In Visual Studio:

  1. Select Debug > Attach to Process...

  1. Locate microstation.exe in the process list.
  2. Select microstation.exe.
  3. Click Select... beside the Attach to field.
  4. Choose:

Managed (.NET Framework 4.x)

  1. Click OK.
  2. Click Attach.

 

Step 4: Load the Add-in

Switch back to MicroStation and enter:

MDL LOAD csAddins2

Press Enter.

When the breakpoint is reached, Visual Studio will pause execution and enter debugging mode.

 

Step 5: Debug the Add-in

Once Visual Studio breaks at the breakpoint, you can use the same debugging tools described in Method 1:

 

Prev: Creating Elements in Add-in Next: Adding Commands to Add-ins