Search code examples
wpf.net-corenugetpublish.net-core-3.1

'Could not load type 'System.IO.Compression.ZipFile' from assembly 'Newtonsoft.Json'


I'm using System.IO.Compression.ZipFile NuGet to extract .zip archive. The code looks like this:

ZipFile.ExtractToDirectory(gameFolderAbsolutePath + api, gameFolderAbsolutePath, true);

There is no issue when using application as compiled in any configuration (Debug / Release). The problem occurs when the application is run after it's publish, with these settings: enter image description here

After this published app is run it crashes at the code line shown above giving this exception message: System.TypeLoadException: 'Could not load type 'System.IO.Compression.ZipFile' from assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.'

I'm not aware of why should have System.IO.Compression.ZipFile look for something in Newtonsoft.Json tho.

Publish.pubx.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>P:\ReleaseCandidate\installer-binaries\data</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>True</PublishSingleFile>
    <PublishReadyToRun>True</PublishReadyToRun>
    <PublishTrimmed>True</PublishTrimmed>
  </PropertyGroup>
</Project>

PackageReferences from .csproj:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
  <PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
  <PackageReference Include="NLog" Version="4.7.3" />
  <PackageReference Include="RestSharp" Version="106.11.4" />
  <PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="106.11.4" />
  <PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
  <PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
  <PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>

Solution

  • Even tho I have tried many times to publish the application with 'Trim unused assemblies (in preview)' checkbox unchecked, it turned out it haven't been taken into process. The problem had been resolved after removing the redundant tags from the .csproj.