System.IO.FileLoadException

LAI TOCA
2 min readJun 7, 2024

--

Create by https://www.bing.com/images/create

Scenario:

We have .Net Standard 2.0 class library that referenced by a application running under .Net Framework 4.8. The library introduced a package called “System.ComponentModel.Annotations” and every time that caused exception (shown below) while the application access to the assembly of class library.

After investigated a while, we found that application seems to loading the assembly (might from Global Assembly Cache) with different version that comparing with the version itself that library included. In order to get rid of this issue we could:

Solution-1: install latest version of “System.ComponentModel.Annotations” on both side of Class library and application(net48), and edit the .csproj setting among <PropertyGroup></PropertyGroup>:

  <PropertyGroup>
...
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
...
</PropertyGroup>

Or just click the below check box under ‘Properties’ tab settings for the application project:

Solution-2: Only install latest version “System.ComponentModel.Annotations” on the class library and edit the csproj file among <PropertyGroup></PropertyGroup>:

  <PropertyGroup>
<TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
...
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
</PropertyGroup>

For the solution of .net framework that host under IIS, we need more further step to tell IIS redirect the library to desire version inside web.config.

<assemblyBinding> 
...
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0"/>
</dependentAssembly>
...
</assemblyBinding>

I would prefer the solution-2 to specific the target frameworks and enable the auto generate binding redirects and we won’t need to install the duplicated packages multiples time across different projects:)

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)