dotnet publish returns Error MSB4062 "Microsoft.Build.Tasks.SGen" task could not be loaded from the assembly
See original GitHub issueWe 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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
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:
From .NET SDK 3.1.102:
If you want to use
sgen.exe
in your build, you must useMSBuild.exe
(or Visual Studio), instead ofdotnet 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 theMicrosoft.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.