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=18.104.22.168, 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:
- Right click on you project in Visual Studio and select Unload
- Right click on your greyed out project and select Edit
- 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.