How to fix 'Could not load file or assembly System.IO.Packaging, Version=4.0.3.0' in a .net standard 2.0 project

8.9k Views Asked by At

I am trying to process a word document. For that, I installed the DocumentFormat.OpenXml NuGet package. It installed following dependencies:

  • DocumentFormat.OpenXml
  • System.IO.Packaging (4.5.0)
  • System.Runtime.Serialization

Here is the Screen shot

As soon as it hit the code where I am processing the word document. It throws this error "Could not load file or assembly 'System.IO.Packaging, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."

I have tried the following things so far:

  • Try to manually add a reference of System.IO.Packaging to CSPROJ file but that didn't work and I got the same error.
  <ItemGroup>
    <PackageReference Include="DocumentFormat.OpenXml" Version="2.9.1" />
    <PackageReference Include="System.IO.Packaging" Version="4.5.0" />
    <PackageReference Include="System.Data.Common" Version="4.3.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
  </ItemGroup>
  • I tried to add "WindowsBase" nuget package but it displayed a conflict:
The type 'Package' exists in both 'System.IO.Packaging, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 

The error is complaining about version 4.0.3.0 but nuget added version 4.5.0 of System.IO.Packaging DLL. How can I fix that?

2

There are 2 best solutions below

1
Shad On

Try removing the reference to System.IO.Packaging (4.5.0) and then try adding the "WindowsBase" nuget package

or

Try

Update-Package –reinstall System.IO.Packaging

0
user On

I had the same issue. I installed WindowsBase from nuget. Then removed WindowsBase package. Then noticed that OpenXml was removed somehow. Then installed OpenXml again the latest version 2.20 and it started working. Now IO.Packaging is under OpenXml package.

enter image description here

Edit: Also .net target framework got updated. enter image description here