Automatically Resolving Secondary Assembly References With MSBuild and TFS Build

There is a known issue/feature with MSBuild which also affects TFS build, since it uses MSBuild to compile, where secondary assembly references are not identified and added to csc and vbc tasks. The exact details are explained here, on MSDN and Stackoverflow with various workarounds and on Microsoft Connect. Visual Studio handles the issues automatically by referencing any secondary references that are required, so you don’t see the problem in Visual Studio but may get compile failures when you run CI or nightly builds.

The exact error that it causes is:

error BC30007: Reference required to assembly 'AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=Token' containing the class 'Namespace.ClassName'. Add one to your project.

The workarounds that are explained in various places around the Internet tend to propose changing the common compile targets for MSBuild or adding the secondary references. Neither are satisfactory since we don’t want to change the way MSBuild works and we don’t want to be aware of the secondary references. There is another workaround which is incredibly simple, override the AfterResolveReferences target which MSBuild provides as an extension point. This is done on a project by project basis since we probably don’t have these issues everywhere – I have only had them when trying to run tests in Continuous Integration.

The steps to edit your project file are:

  1. Right click on you project in Visual Studio and select Unload
  2. Right click on your greyed out project and select Edit
  3. Paste the following code into the file before the last </Project> tag
<Target Name="AfterResolveReferences">
    <!-- Redefine referencepath to add dependencies-->
    <ItemGroup>
      <ReferencePath Include="@(ReferenceDependencyPaths)"></ReferencePath>
    </ItemGroup>
  </Target>

Your project should now compile without issues from the command line with MSBuild and with TFS Build.

2 Responses to “Automatically Resolving Secondary Assembly References With MSBuild and TFS Build”

Read below or add a comment...

  1. Jim Rogers says:

    This worked great for me on the build server, but Visual Studio 2012 started complaining about the projects I had put this in. I got this error:

    A problem occurred while trying to set the “References” parameter for the IDE’s in-process compiler. Error HRESULT E_FAIL has been returned from a call to a COM component.

    This may only happen in conjunction with compiling specifically to x86, or some other setting in our solution; unfortunately I couldn’t find a way out of it, and I’m having to go back to just referencing those secondary dependencies.

  2. Jim Rogers says:

    I posted too soon! You can force Visual Studio to ignore this completely by putting a condition in the ItemGroup:

    In this case the opposite of true is not false; I think it’s empty string on the build server. The not equals condition does the trick. This does the same thing on the build server but produces no error when compiling in Visual Studio.

Leave A Comment...

*