9

Win7, VS2017, ASP Net core app, target framework is 4.6.

When I try to build my project with CLI (it is needed before dotnet commands calling) the error occurs:

C:...\Microsoft.Common.CurrentVersion.targets(2547,5): error MSB4062: The "Microsoft.Build.T asks.ResolveComReference" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework .ITask. [C:\project.csproj]

I have been looking for questions related with my issue and have done the following things:

  • restart VS
  • clean and build the project with VS tools (it was successfully)
  • load Microsoft.Build package
  • remove content of packages folder and reload it

But nothing helps me.

What it can be?

Ilya Loskutov
  • 1,967
  • 2
  • 20
  • 34

1 Answers1

24

COM references are not supported by the .net core version of MSBuild which is what the .NET CLI uses. For those references to work, you have to build your project using msbuild directly. Using the Developer Command Prompt for Visual Studio 2017 you should be able to perform the build using commands like:

msbuild /t:Restore
msbuild /t:Build /p:Configuration=Release
msbuild /t:Publish /p:Configuration=Release

(you can also add /m to enable parallel builds).

This reference is also unsupported when running CLI tools that use these project files since they also rely on the .NET Core version of MSBuild.

However, there is a workaround that will enable CLI tools to be able to use the project, but it will not allow them to fully build using the .NET Cli. Add the following Condition attribute to your ComReference items to "hide" the COM reference when building with the .NET Core version of MSBuild:

<COMReference Include="FooBar" Condition="'$(MSBuildRuntimeType)' != 'Core'">
  <Guid>{some-guid}</Guid>
  <!-- a lot more metadata elements -->
</COMReference>
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 3
    Thank, Martin, your answer is very helping for me. I opened my .csproj file and found some com references there (COMReference sections). Just removing those sections is a solution – Ilya Loskutov May 22 '17 at 19:52
  • 2
    Probably depends on whether or not your project actually needs those COM references.. – Martin Ullrich May 22 '17 at 19:58
  • Relevant [github issue](https://github.com/dotnet/msbuild/issues/3986). – Adam Caviness Aug 01 '20 at 21:23
  • 1
    this just gives me compilation errors for the missing types instead – Dave Cousineau Oct 26 '20 at 22:42
  • @MartinUllrich Thanks, after much of hardwork finally I did by editing project file and commenting the ommRef.. secion – DareDevil Feb 10 '21 at 14:43
  • Thank you so much Martin, this helped me.. i saw the COMReference block and I commented them out... Now my App builds and works well... Been battling with this for a while – Oluwatobiloba Jun 21 '21 at 18:56
  • Thanks for answer. One thing to note: You don't strictly need the "Developer Command Prompt for Visual Studio", you can just fix the path to msbuild.exe and build it from any command prompt. I found the msbuild path using this answer and it worked: https://stackoverflow.com/a/53319707/350384 – Mariusz Pawelski Jun 13 '23 at 11:13