Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build error "An attempt was made to load an assembly with an incorrect format " after upgrade net framework 4.5.2 to 4.7.1

Tags:

c#

.net-4.7.1

We are trying to update the framework of our program. We currently have it in version 4.5.2 and we want to update it to version 4.7.1

We have changed all the csproj of the solution, and when we compile in debug, the application compiles and works correctly. But when we do it in release, it fails us with the following error:

An attempt was made to load an assembly with an incorrect format: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\Facades\System.IO.Compression.ZipFile.dll

We don't really know what's wrong, does anyone know what it could be?

Thank you very much.

like image 832
Meldrel Avatar asked Mar 15 '18 15:03

Meldrel


People also ask

Why am I getting MSBuild errors?

This topic describes MSBuild errors that might occur because of reference issues and how you can resolve those errors. You can create applications that reference projects or assemblies that target different versions of the .NET Framework.

Why am I getting an assembly reference error in my project?

That incorrect entry causes the assembly reference to not correctly unify with the assembly in the framework and leads to that error. This is documented as a known issue in .NET Framework 4.7.1. As a workaround you can add these targets to your project.

Why is my program loading with an incorrect format?

An attempt was made to load a program with an incorrect format. 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.

Why is my Assembly not unifying with the framework?

The root of the issue is that the assembly you are seeing in the error message has an incorrect entry in the .NET Framework unification table. That incorrect entry causes the assembly reference to not correctly unify with the assembly in the framework and leads to that error. This is documented as a known issue in .NET Framework 4.7.1.


3 Answers

UPDATE: As Josh suggests below, now that 4.7.2 is available, upgrade to that .NET version for the best resolution of this problem.

If stuck with 4.7.1: This probably isn't addressing the root of the problem, but if you want to get over this for the moment, then find the offending project and edit its settings (rclick project, 'Properties', then 'Build' tab.)

Set 'Generate serialization assemblies' to 'Off' for Release mode.

If it still complains, try adding the following <Target>s to your .csproj file (e.g. towards the bottom, just inside the enclosing </Project> root tag:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
  </Target>
  <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
      <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
  </Target>
like image 90
oflahero Avatar answered Oct 16 '22 11:10

oflahero


This issue is fixed in the latest .net dev pack 4.7.2:

https://github.com/dotnet/sdk/issues/1630#issuecomment-415811457

https://www.microsoft.com/net/download/thank-you/net472-developer-pack

like image 21
josh Avatar answered Oct 16 '22 12:10

josh


The root of the issue is that the assembly you are seeing in the error message has an incorrect entry in the .NET Framework unification table.

That incorrect entry causes the assembly reference to not correctly unify with the assembly in the framework and leads to that error. This is documented as a known issue in .NET Framework 4.7.1.

As a workaround you can add these targets to your project. They will remove the DesignFacadesToFilter from the list of references passed to SGEN (and add them back once SGEN is done)

    <Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
      <ItemGroup>
        <DesignFacadesToFilter Include="System.IO.Compression.ZipFile" />
        <_FilterOutFromReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" 
            Condition="'@(DesignFacadesToFilter)' == '@(_DesignTimeFacadeAssemblies_Names)' and '%(Identity)' != ''" /> 
        <ReferencePath Remove="@(_FilterOutFromReferencePath)" />
      </ItemGroup>
      <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." /> </Target>

    <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
      <ItemGroup>
        <ReferencePath Include="@(_FilterOutFromReferencePath)" />
      </ItemGroup>
      <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has ran." />
    </Target>

Edit: If the above doesn't work, please share a detailed msbuild log to help understand why the target doesn't work.

Another option (machine wide) is to add the following binding redirect to sgen.exe.config:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.IO.Compression.ZipFile" publicKeyToken="b77a5c561934e089" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>

This will only work on machines with .NET Framework 4.7.1. installed. Once .NET Framework 4.7.2 is installed on that machine, this workaround should be removed.

like image 4
Alex Ghiondea - MSFT Avatar answered Oct 16 '22 12:10

Alex Ghiondea - MSFT