Type or namespace not found “are you missing assem

2019-02-12 10:20发布

I am trying to use MSBuildWorkspace class . I have all assembly references in my project. When I open the reference in object browser, I see the namespace and the class I am trying to use. But in my following using statement,

using Microsoft.CodeAnalysis.MSBuild

I am getting a

The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)

But funny that Syntax highlighter recognizes the type name, its the compiler complaining

enter image description here

Here is the build log

   1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): 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.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.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.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.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.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): 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.Framework, 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.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, 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.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, 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.
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,36,37,43): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,96,37,103): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

3条回答
聊天终结者
2楼-- · 2019-02-12 11:05

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.

查看更多
Ridiculous、
3楼-- · 2019-02-12 11:06

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-02-12 11:07

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.

查看更多
登录 后发表回答