14

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 ==========
user2864740
  • 60,010
  • 15
  • 145
  • 220
fahadash
  • 3,133
  • 1
  • 30
  • 59
  • 2
    Have you tried rebuilding/cleaning/deleting assembly referenc and adding it again? – Dávid Kaya May 12 '14 at 20:40
  • 1
    Are you targeting framework 4.5 and have it set to client profile instead? Something along those lines...? – Dave Ziegler May 12 '14 at 20:42
  • It is set to use 4.5. I don't see 4.5 Client Profile as an option. I tried 4 Client Profile and now it can't even recognize CodeAnalysis to be in Microsoft namespace. – fahadash May 12 '14 at 20:45
  • We don't support client profile. Please include in your build log any warnings from the compiler in addition to errors. – Jason Malinowski May 12 '14 at 20:55
  • @JasonMalinowski I just did – fahadash May 12 '14 at 21:02
  • It looks like you have a version compatibility problem. If you're sure the Microsoft.Build and Microsoft.Build.Framework assemblies are backwardly compatible, you could try forcing the version numbers to match. – Evil Dog Pie May 12 '14 at 21:19
  • I also got same error for my component. I'd create one component using .net 4.5 framework and another team trying to consume my component in one of existing windows console apps which was running .net 4.0 client profile. They told me it's not compiling, their tried remove ref., add again, clean solution and tried and tired. After i looking sometime finally we found that due to .net version conflicts. Later i changed .net 4.5 in console apps and started compiled properly. – Jaisankar Feb 11 '15 at 07:25

3 Answers3

11

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.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • It is set to 4.5. I had never been on 4. I create a new WPF Application using my VS2012 which defaults it to 4.5. Then I add Microsoft.CodeAnalysis through Nuget and I still get this error. If you guys are pushing us to pay more money to Microsoft and upgrade to 2013, this is not fair. – fahadash May 13 '14 at 07:35
  • 2
    Nope, we're not doing this to make more money. As we work on Roslyn, we need to take dependencies on newer components of the framework and Visual Studio to do some awesome new features. Maintaining builds that both let us target new features _and_ also still work on older versions becomes more trouble than it's worth. – Jason Malinowski May 13 '14 at 17:16
  • If you're not trying to extend Visual Studio, the express editions should be more than sufficient for your needs. You could also try to install the [MSBuild standalone package](http://www.microsoft.com/en-us/download/details.aspx?id=40760) which will install the 12.0 assemblies, and then do the SpecificVersion stuff to try forcing VS to use the 12.0 assemblies. I haven't tried that so I can't exactly vouch if it'll work. – Jason Malinowski May 13 '14 at 17:19
  • 1
    @JasonMalinowski, can you comment on fahadash's answer below? It seems to work perfectly and everything I'm trying to use Microsoft.CodeAnalysis for appears to work. (the goal being using Microsoft.CodeAnalysis in VS 2012) Since I'm targeting .Net Framework 4.5.1 in either scenario, I'm curious why using VS 2012 would be fraught with peril? – Kirk Woll Jun 17 '14 at 15:34
  • The tricky problem is when we take dependencies on new APIs. For example, Roslyn is moving to take a depenedncy on some new MSBuild APIs that will only be present in version 14.0 or later. The SpecificVersion stuff will mean you build just fine, but if you end up calling into us and we can't find the new APIs, we'll crash at runtime. The potential danger of silencing reference warnings is although it might build, it might not run. But maybe in your scenarios with Roslyn, it won't matter. – Jason Malinowski Jun 18 '14 at 00:47
  • 3
    There must be another cause for this error. I'm experiencing it with Microsoft.Build.Framwork, and I'm using vs2013 targeting 4.5.1 in all my projects, building on tfs2013. – rickstockham Sep 29 '14 at 16:47
  • I'm seeing behavior similar to rickstockham. All my projects are targeting 4.5.1, but I'm getting this error on the TFS builds. It's worth noting that it all builds just fine on my local machine, and was working on the build server up until yesterday. I updates some NuGet references, and started having problems. I've tried rolling them back, but the problem seems permanent at this point. How can I trace down which reference is the cause? – Mel Jan 14 '15 at 14:19
8

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.

fahadash
  • 3,133
  • 1
  • 30
  • 59
  • 1
    I know you found the answer on another blog. But can you please add a little more detail to your answer in what you did. Just in case the website is dead in the future, the answer will be here. – Black Frog May 12 '14 at 21:22
  • 2
    Summarize and/or extract relevant information from external resources. If the link/blog became inaccessible, people (reading this "answer") would not know that the key to the proposed solution is using a Specific Version .. – user2864740 May 12 '14 at 21:23
  • 1
    Just did. Thanks for bringing it up. – fahadash May 12 '14 at 22:42
  • This worked for me, as the other answer was not correct since I was already targeting 4.5.1. – Paul Jun 18 '15 at 20:26
0

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.

alongshot
  • 1
  • 1