msbuild: SGen doesn't work with reference assemblies (?)
Steps to reproduce
- Create a new Windows Classic Console App in VS 15.3 or newer
- Unload the project and add these package references to the .csproj:
<ItemGroup>
<PackageReference Include="MassTransit.RabbitMQ">
<Version>3.5.6</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>1.1.0</Version>
</PackageReference>
</ItemGroup>
- Set this property:
<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
- Reload the project and build.
Expected behavior
Should build.
Actual behavior
Severity Code Description Project File Line Suppression State
Error An attempt was made to load an assembly with an incorrect format: C:\Users\kirillo\.nuget\packages\System.Net.Http\4.3.0\ref\net46\System.Net.Http.dll. DanTupRepro C:\Users\kirillo\Documents\Visual Studio 2017\Projects\DanTupRepro\SGEN
Environment data
msbuild /version output:
15.3.409.57025
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 17
- Comments: 29 (6 by maintainers)
Hi folks, we still hit this issue in 2024 on our .net 4.8 project with errors like on a few packages:
SGEN : error : An attempt was made to load an assembly with an incorrect format: D:\NuGet\system.buffers\4.5.1\ref\net45\System.Buffers.dllThe workaround using
ExcludeAssets="compile"only worked for some packages which we don’t explicitly reference, but we do have some explicitly referenced packages required to compile our code. So this workaround didn’t work for us.@toebens’s workaround is a bit too heavy for us but shined a little light for us. Note that path of the reference assemblies all contain
\ref\***, so we adopted a simpler way to exclude them from ReferencePath.Our way is to add this new target into the csproj file having issue when
GenerateSerializationAssembliesis on:Since both this bug and https://github.com/dotnet/wcf/issues/3991 were closed with no real fix. Hope this tip could help someone in future and save time.
I’ve submitted a ticket in the SDK: https://github.com/dotnet/sdk/issues/1630 and proposed a work around that we use in my company.
Well, ‘me too’. We are using .net 4.7.2 and both new and old format of .csproj files in different projects. Error occurred after referencing new package.
Me too.
same error
Severity Code Description Project File Line Suppression State Error An attempt was made to load an assembly with an incorrect format: C:\Users\ey02.nuget\packages\system.memory\4.5.1\ref\netstandard2.0\System.Memory.dll. SapTalk.Scheduler C:\Workspace\SourceCode\Phase5\Ey.SapTalk\SapTalk.Scheduler\SGEN
me too. i also met the same issue in the vs 2019, .net framework 4.7.2. error message as below:
Severity Code Description Project File Line Suppression State Error An attempt was made to load an assembly with an incorrect format: C:\Users\ey02.nuget\packages\system.memory\4.5.1\ref\netstandard2.0\System.Memory.dll. SapTalk.Scheduler C:\Workspace\SourceCode\Phase5\Ey.SapTalk\SapTalk.Scheduler\SGEN
Workaround for me is
ExcludeAssets="compile":Is that a bug in the package? It shouldn’t be adding references to my project, only messing with build targets to pass
/embed:bigfilelistto csc.exe.It does:
https://github.com/microsoft/msbuild/blob/master/src/Tasks/Microsoft.Common.CurrentVersion.targets#L3429-L3431
my bad workaround looks like this currently. i overwrote the target after the import of all usual targets like this in my .csproj file (you might want to change the regex):
however this is very bad. Microsoft please come up with a real fix ASAP
@rainersigwald
The program
sgen.exeis owned by the WCF folks. They own both the .NET Framework version and any future .NET Core work. We talked to them internally and agreed to move the public bug to their repo (even though that’s not where a fix would be checked in, it is at least the right people).Same issue with 4.8.
This is still happening for certain projects here as well, is there any other workaround besides turning off that serialization?
This will only get more common as people move to new-style projects + NuGet. Pulling into 15.6, at least for triage so we don’t forget it.