question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotnet publish returns Error MSB4062 "Microsoft.Build.Tasks.SGen" task could not be loaded from the assembly

See original GitHub issue

We have an ASP.NET Core 2.1 application using the full framework (4.7) that references a .NET 4.6 class library that make SOAP calls (WebReference). Since 2016 we’ve been able to run dotnet publish in our CI process with no issues with VS 2017. Once we installed VS2019 on the same host this stopped working.

We suspect it’s a problem with the dotnet CLI deciding which version of MSBuild to run.

We understand that there are some work-arounds that seem to get past it such as setting <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies> (which we don’t want to do) and it seems that running MSBuild directly instead of dotnet publish works but we wanted to report this issue here.

Steps to reproduce

  • Use a windows machine with VS 2017 Pro (v15.9.11) and VS 2019 Pro (16.0.2) installed, both with web workloads
  • Create a solution with two projects:
    • ASP.NET Core 2.1 with .NET Framework 4.7 which references a…
    • .NET 4.6 Class Library Project
  • Add a Web Reference to the class library project (i.e.http://www.dneonline.com/calculator.asmx)
  • Run dotnet publish --configuration Release -out c:\temp\foo

Here is a repo with an example.

Expected behavior

The publish operation works

Actual behavior

This error is thrown:

c:\code\sandbox\PublishBugExample\PublishBugExample>dotnet publish --configuration Release -o c:\temp\bug-demo
Microsoft (R) Build Engine version 16.0.450+ga8dc7f1d34 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 65.03 ms for c:\code\sandbox\PublishBugExample\PublishBugExample\PublishBugExample.csproj.
C:\Program Files\dotnet\sdk\2.1.602\Microsoft.Common.CurrentVersion.targets(3541,5): error MSB4062: The "Microsoft.Build.Tasks.SGen" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.  Confirm that the <UsingTask> 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:\code\sandbox\PublishBugExample\ExampleWebServiceLib\ExampleWebServiceLib.csproj]

More info

It seems that running MSBuild works however and the publish output is almost identical compared to when dotnet publish was ran in the past:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\msbuild" /t:Publish /p:Configuration=Release;Out-Dir=C:\temp\foo

According to our build logs, the last successful time dotnet publish ran (before VS 2019 was installed), it reported using Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core you’ll notice in the output above it reports using 16.0.450+ga8dc7f1d34 for .NET Core.

If dotnet publish is not the recommended way of publishing such configurations please provide guidance.

Environment data

dotnet --info output:

.NET Core SDK (reflecting any global.json):
 Version:   2.1.602
 Commit:    04475eb7cf

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.602\

Host (useful for support):
  Version: 2.1.9
  Commit:  dcedc87d22

.NET Core SDKs installed:
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.1.502 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
daniel-chamberscommented, Mar 4, 2020

We’re having this issue with a dotnet build -c Release. In a release build, the SGen task is used to generate the XML serialization assembly, but not for Debug, so a Debug build succeeds, but a Release build fails.

However, if we build in Visual Studio 2019 instead of from the command line via dotnet, a Release build works.

We did some investigation and it seems the problem is that the version of the Microsoft.Build.Tasks.Core.dll that ships with .NET SDK does not contain the SGen task class at all, however, the same assembly included in Visual Studio 2019 does, which is why it builds in Visual Studio and not from the command line. This has to be a bug; surely they should be the same assemblies!

Version numbers for reference:

From Visual Studio 16.4.5: image

From .NET SDK 3.1.102: image

0reactions
rainersigwaldcommented, Mar 11, 2020

If you want to use sgen.exe in your build, you must use MSBuild.exe (or Visual Studio), instead of dotnet build.

SGen.exe is part of the old .NET Framework SDK and hasn’t been ported to .NET Core. The team that owns it does not plan to do so; the replacement is the Microsoft.XmlSerializer.Generator package.

The error message should be much clearer, though. I moved https://github.com/microsoft/msbuild/issues/3583 into the next release; hopefully we can get that going soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MSB4062 - MSBuild
This error is emitted when a task assembly cannot be loaded prior to executing a task. Error MSB4018 may be caused when running...
Read more >
NET Core error on build: error MSB4062 Microsoft.Build. ...
UPDATE: Seems like the dotnet CLI does not support COM references. Visual Studio uses msbuild directly behind the scenes, but on the build...
Read more >
Did you know that you can build .NET Core apps with MSBuild ...
Common.CurrentVersion.targets(3639,5): error MSB4062: The "Microsoft.Build.Tasks.AL" task could not be loaded from the assembly Microsoft ...
Read more >
Can't build a .NET Core project using dotnet publish when ...
CurrentVersion.targets(3164,5): error MSB4062: The "Microsoft.Build.Tasks.LC" task could not be loaded from the assembly Microsoft.Build.
Read more >
Build Error About sgen.exe - Progress Community
When build the project an MSBuild error related to SGen are thrown in Visual ... The specified task executable "sgen.exe" could not be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found