Type or namespace not found “are you missing assembly reference” while all references are correct

南楼画角 提交于 2019-11-30 17:50:52

So this:

warning MSB3258: The primary reference "Microsoft.CodeAnalysis.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.

Means you are building with your project targeting the .NET 4.0 framework. You should be targeting 4.5.1 with Visual Studio 2013. Other configurations are unsupported. I do not recommend trying to "force" this by silencing the warning -- that can just cause issues down the road. Roslyn uses APIs added in 4.5, so you will have troubles trying to silence the issue.

I found this Blog post from Nansen and I applied the fix and got my issue resolved.

Summary of the solution: Edit the csproj file in XML editor and find the elements for the references that are troubling you and add the following child element to those.

<SpecificVersion>True</SpecificVersion>

Make sure the word True is only first letter uppercase (True, not true or TRUE).

Save and reload the project in VS and build it.

I had the same build error message generated from the build server when using a project reference inside my startup project - it worked fine when building through the VS2013 IDE:

The type or namespace name 'XYZ' could not be found (are you missing a using directive or an assembly reference?)

After running MSBuild.exe locally instead of in the build server, I was able to duplicate the error. Examining the .csproj file for the project reference by unloading it from the solution, I noticed that I had the following line on top:

<Project DefaultTargets="Configure" xmlns="..." ToolsVersion="12.0">

I updated it with this line following the exact syntax of the startup project:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="...">

It turns out that the DefaultTargets property had to be the same. That's the reason why the project reference was not being included with the '/reference' paramenter when trying to compile the project with the CSC.exe command after running MSBuild.exe.

It took me a while to figure out a solution to this problem since I couldn't find anything similar on any forum. This is a legacy application that has always been built locally instead of in a build server.

By the way, using PSBuild (PowerShell interface to MSBuild) together with Markdown Pad 2 works great locally when trying to troubleshoot build errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!