Thursday, May 5, 2011

Debugging in Visual C# 2010 Express

When you try to debug a Revit add-in by selecting "Start Debugging" from the "Debug" menu (or by hitting the "F5" key) you will prompted with an error saying that "A project with an Output Type of Class Library cannot be started directly.".

While this is true as you can't run your add-in without it first being loaded into Revit it is also a bit frustrating. In the full paid version of Visual Studio there are options to specify the starting application for class libraries allowing you to debug your add-in, these options are missing from the Express versions. There is a workaround though, see below:

When you start a project, Visual C# 2010 Express automatically creates the files needed to create your application or add-in, one of these files is named "YourProject.csproj.user", by editing this file you will be able to specify the starting application for your add-in.

When you open this file in Visual C# it appears like this:
<?xml version="1.0" encoding="utf-8"?>
<project toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</project>
Simply add a new "PropertyGroup" tag between the "Project" opening and closing tags so that the file now looks like this:
<?xml version="1.0" encoding="utf-8"?>
<project toolsversion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <propertygroup condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <startaction>Program</startaction>
    <startprogram>C:\Program Files\Autodesk\Revit Architecture 2012\Program\Revit.exe</startprogram>
  </propertygroup>
</project>
Close and then re-open your solution and you can now debug your add-in!

Note that the path shown in the xml above within the "StartProgram" tag relates to the default location for Revit Architecture 2012, you will need to change this path if you are using a different version or have installed in a different location.

6 comments:

  1. Thank you!!!!!!!!!!!!!!!
    Thank You!!!
    Its working finally !!!

    ReplyDelete
  2. The Autodesk tutorial "My First Plug-in" instructs to place your above code in the "YourProject.csproj" file, not the "YourProject.csproj.user" file. I do not understand the difference. Unfortunately I am unable to get it to work trying either method. I get the same error referred to above.

    ReplyDelete
  3. Hi n8,

    Are you able to give more details about the steps you are taking? This should work.

    ReplyDelete
  4. Hi, I'm trying to run one of the samples in the SDK, when I get this message. However, I don't know where to find this file "YourProject.csproj.user". In order for me to edit it, how do I locate it? It isn't there in my samples folder. I'm using Revit 2014.
    Thanks.

    ReplyDelete
  5. Hi Srinath, you can also add the necessary lines to the "YourProject.csproj" file... or you can create a "YourProject.csproj.user" file yourself from a blank file.
    Note that you will need to have the .addin file in place or Revit will not find your (or the sample) plugin.

    ReplyDelete
  6. It is working now. Thank you so so much.

    ReplyDelete