Can MSBuild no longer find dependencies after upgrading from 4.0 to 4.5?

We have a .Net 3.5 application that is created using some scripts that call msbuild.exe

Recently, all of our machines began to automatically upgrade from .Net 4.0 to .Net 4.5 as part of the company's entire policy, and our build scripts started to fail.

The errors mentioned are that they cannot find a link to the assembly, for example:

  error CS0012: The type 'System.Drawing.Image' is defined in an assembly that is not referenced. 
 You must add a reference to assembly 'System.Drawing, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'.

It seems that in each case we are trying to build project A, which has a link to project B, and project B has a link to library X and gets an error for which project A needs a link to library X.

The workaround is to uninstall 4.5, uninstall 4.0, and then reinstall 4.0, however this is time consuming and impractical in an environment where updates are often silent and automatic.

I already tried using the following msbuild switches, no luck

  • /toolsversion:3.5 - Exception near Func undefined
  • /toolsversion:4.0 - Doesn't work because 4.5 replaced 4.0 tools
  • /p:TargetFrameworkVersion="v3.5" - Same errors
  • /p:VisualStudioVersion=11.0 - same error * `in csproj file - same error already exists

The .csproj files already have <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> specified in each csproj file in my solution, and ToolsVersion="4.0" .

I also tried changing TargetFrameworkVersion to 4.0, and this does not work either.

I found several notes on the Internet that Microsoft changes the OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration flag from false to true during the update 4.0 to 4.5, however manually this flag in csproj files also does not fix the problem.

 <OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration> false </OnlyReferenceAndBuildProjectsEnabledInSolutionConfiguration> 

Why msbuild cannot find these child assemblies after upgrading from 4.0 to 4.5 and how can I fix it?

Update

I finally figured out the source of the problem, but I have no idea how this can happen.

ProjectA has a class that inherits from an abstract class in ProjectB, and one of the properties of ProejctB.BaseClass is of type System.Drawing.Image .

 namespace ProjectA { public class SomeClass : BaseClass { } } namespace ProjectB { public abstract class BaseClass { public System.Drawing.Image GetImage() { }; } } 

From everything that I read on the Internet, and from creating my own test project, this means that ProjectA needs a reference to System.Drawing.Image for the assembly.

But for some reason this limitation does not seem to matter for our project when starting with msbuild 4.0 or VS VS. He gladly creates ProjectA without reference to System.Drawing.Image .

Even after upgrading to .Net 4.5, I can still successfully build ProjectA from Visual Studio 2010 without adding a link to System.Drawing.Image , however the creation using msbuild now (correctly?) Fails. I also get support errors when building in VS 2012 Express, so everything that allows me to do this seems to have been fixed in newer versions of VS.

Currently, I went through all 148 projects in our solution and fixed all the links, however I want to leave this question open in order to try to get an answer on why I can build ProjectA using msbuild 4.0 or Visual Studio 2010 without a link to System.Drawing.Image .

I have already determined that I cannot easily reproduce the behavior in the test project, so I best assume that this is either some kind of configuration, part of the build scripts, or an error that existed back when .sln or .csproj files were created and no longer exist in newly created projects.

+5
source share
4 answers

Based on Hans comment, this sounds like something has changed in the internal C # compiler between version 4 and version 5 to fix the error that allowed us to create ProjectA without the appropriate links.

This has nothing to do with MSBuild, which has changed in that you are using a different C # compiler. Version 5 instead of 4. I saw cursory evidence on other issues that are more aggressive about the desire to allow indirect links. Nothing is nailed to the wall; everyone just fixes it with an obvious solution.

When I look at the msbuild.exe I see that it comes down to calling csc.exe , which contains the same list /reference , but 4.5 does not work with an error.

Building ProjectA with 4.0

  Task "Csc"
   c: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Csc.exe / noconfig / nowarn: 1701,1702 / nostdlib + / errorreport: prompt / warn: 4 / define: TRACE / reference: C: \ Path \ bin \ Release \ ProjectB.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll / reference: "C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Core.dll "/ reference:" C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Data.DataSetExtensions.dll "/ reference: C: \ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll / reference: C: \ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ System.Web.Services.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll / reference: "C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Xml.Linq.dll "/ debug: pdbonly / filealign: 512 /keyfile:ProjectA.snk / optimize + /out:obj\Release\ProjectA.dll / target: library Properties \ AssemblyInfo.cs SomeF  ile.cs Properties \ VersionInfo.cs
   Microsoft (R) Visual C # 2010 Compiler version 4.0.30319.1
   Copyright (C) Microsoft Corporation.  All rights reserved.

 Done executing task "Csc".

Building ProjectA with 4.5

  Task "Csc" (TaskId: 5812)
   C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Csc.exe / noconfig / nowarn: 1701,1702 / nostdlib + / errorreport: prompt / warn: 4 / define: TRACE / highentropyva- / reference: C: \ Path \ bin \ Release \ ProjectB.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll / reference: "C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3 .5 \ System.Core.dll "/ reference:" C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Data.DataSetExtensions.dll "/ reference: C: \ Windows \ Microsoft .NET \ Framework \ v2.0.50727 \ System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2 .0.50727 \ System.Web.Services.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll / reference: "C: \ Program Files (x86) \ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Xml.Linq.dll "/ debug: pdbonly / filealign: 512 /keyfile:ProjectA.snk / optimize + /out:obj\Release\ProjectA.dll / target: library / utf8output Prop  erties \ AssemblyInfo.cs SomeFile.cs Properties \ VersionInfo.cs (TaskId: 5812)
   Microsoft (R) Visual C # Compiler version 4.0.30319.18408 (TaskId: 5812)
    (TaskId: 5812)
   for Microsoft (R) .NET Framework 4.5 (TaskId: 5812)
   Copyright (C) Microsoft Corporation.  All rights reserved.  (TaskId: 5812)
    (TaskId: 5812)
 SomeFile.cs (32,18): error CS0012: The type 'System.Drawing.Image' is defined in an assembly that is not referenced.  You must add a reference to assembly 'System.Drawing, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'.  [C: \ Path \ ProjectA.csproj]
   c: \ Path \ bin \ Release \ ProjectB.dll: (Location of symbol related to previous error) (TaskId: 5812)
   The command exited with code 1. (TaskId: 5812)
 Done executing task "Csc" - FAILED.  (TaskId: 5812)

I was hoping someone here would be able to explain what it was and how I could reproduce this β€œerror” with 4.0, however it does not seem like this question is easy to answer.

+1
source

If I were to guess, I will say that all the links that he can no longer find are version 2.0.0.0. We recently had a similar problem when we had to update links to version 4.0.0.0 of the System. * And mscorlib and related links.

If you cannot replace the links and BindingRedirects is not an option, try adding v2.0.0.0 manually to the bin folder.

Finally, you can make the build's output rigorous to see where it looks for these links, it can help you find a solution.

0
source

Perhaps when it updates it without updating your execution parameters diagram, in particular the <probing> element that tells .Net which paths to search for assemblies:

Eg.

 <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="bin;bin2\subbin;bin3"/> </assemblyBinding> </runtime> </configuration> 

See: http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.assignprojectconfiguration.onlyreferenceandbuildprojectsenabledinsolutionconfiguration%28v=vs.121%29.aspx

0
source

Rachel, are any random links to your System.Drawing on projects set to SpecificVersion = true after the update?

Also, do you use any pre-compiled resx files in your library projects? Try right-clicking the .resx file and selecting Run Custom Tool again to redo the links.

And for the latest attempts to help you, you can try to use useLegacyV2RuntimeActivationPolicy="true" in your main project configuration and try to use the outdated System.Drawing library.

0
source

Source: https://habr.com/ru/post/1205806/


All Articles