Run Configurations: debug any static method in Visual Studio, and more

Sometimes when you’re writing code, you want to verify an assumption quickly, check how your API works, or simply prototype and execute a small part of your code.

What common options are available to us?

  1. Create a separate small project, put the code to verify into its Main() method, and add all the necessary dependencies.
  2. Hook the code to verify to a random button and start the debugger (and hopefully remember to remove the code afterwards).
  3. Write a temporary unit test and execute it with the Unit Test Runner.

Well, with ReSharper 9.2’s new run configurations there’s now a better way.

With the caret on a static method without parameters, press Alt+Enter to see a context action called Debug:

ReSharper's Debug context action

Calling this action triggers debugging, with this very static method used as an entry point. In other words, without breaking your coding flow, you can write a public static void SomeTestMethod() right where you’re coding and quickly launch the debugger.

Under the hood, this context action is based on a new feature, run configurations. The feature grabs information required to execute from the current project, creates an implicit run configuration, and actually executes it.

The Debug menu item can be expanded to reveal the entire set of available actions: Debug, Run and Profile (available if dotTrace is installed), each in two varieties: with or without building. Each of the expanded actions opens a dialog box where you can refine configuration parameters as necessary, and execute the configuration with or without saving it for the future.

Run or debug any static method without parameters

When you choose to save and execute, your settings are persisted so that you can reuse them later. There are three ways to access a saved configuration:

    • A pinned gutter icon next to a static method
      Gutter icon for a saved run configuration
    • The Run Configurations pop-up that is available by selecting ReSharper | Tools | Run Configurations or with a shortcut (Ctrl+Shift+Alt+R in both Visual Studio and IntelliJ keymaps)
      Run Configurations popup
    • If you have dotTrace integrated into Visual Studio, you can also access run configurations via its Performance Snapshots Browser (ReSharper | Profile | Show Performance Snapshots).
      Performance Snapshots Browser

    ReSharper 9.2 provides three kinds of run configurations:

    • Executable: Allows running any executable file.
    • Static Method: As suggested above, this lets you run any static method that doesn’t have parameters.
    • Project: Allows executing a Visual Studio project (which in essence emulates setting a project as startup and launching it immediately).

    You can add any of these configurations via the Add… submenu in the Run Configurations pop-up.

    In addition, there is a new item in the project context menu, Create Run Configuration, which creates a new project run configuration.

    The Run Configurations pop-up provides quick keyboard access to configurations. It allows you to execute any run configuration in any supported execution mode. Each configuration’s submenu provides a set of Configure commands.

    A run confgiration's Configure menu

    One of these commands, Select, deserves a notable mention. Select assigns the current configuration to launch when you invoke Visual Studio’s own Start (F5) and Start Without Debugging (Ctrl+F5) commands. In addition, two Alt-modified shortcuts are added which affect whether or not a solution is built before launch:

    • Alt+F5: Debug without building
    • Ctrl+Alt+F5: Execute a configuration without debugging and building

    A setting called Don’t build by default that concludes the Run Configurations pop-up serves to swap the default and Alt-modified actions. For example, it makes F5 invoke debugging without building.

    Other Configure options include:

    • Edit, Duplicate and Delete: Fairly self-descriptive.
    • Share/Unshare: Saves a run configuration to shared solution settings (instead of the default personal solution settings), which enables sharing the run configuration among all developers working on the current solution.
This entry was posted in How-To's and tagged , , . Bookmark the permalink.

6 Responses to Run Configurations: debug any static method in Visual Studio, and more

  1. Pingback: Dew Drop – August 28, 2015 (#2079) | Morning Dew

  2. Sergiy says:

    Great feature, could you also add test case to the possible launch target? Executing specific test case with F5 would be really useful.

  3. Pingback: Dew Drop – August 31, 2015 (#2080) | Morning Dew

  4. Lev Lehn says:

    Do you have plans supporting several startup projects or attaching a new process to the debugger when it is already running? (which would enable to debug several processes when run through run configurations) This would make that a killer feature IMHO.

    We have a rather large solution, and today each developer who starts working with it, needs to do this:
    -configure multipe startup projects
    -for each of those, configure command line arguments
    -when debugging a different set of services, reconfigure startup projects
    this should be something completely avoidable in 2015, unfortunately VS devs rarely seem to look out to other tools (eclipse had run configurations for centuries). With the possibility to share run configurations in a team config, we could finally do this in VS too.

    Please add this.

Leave a Reply

Your email address will not be published. Required fields are marked *