0

I'm working on an application with a GUI and a Powershell module in the same solution. On Windows 2008 R2 framework 4.5.2 I have the error message :

System.TypeInitializationException: The type initializer for 'ClassName' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

I fixed it quickly on the GUI module by using in the app.config :

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
  </runtime>

But no way to fix it on the powershell dll module with the same fix :

I tried to add the System.Net.Http nuget package, removing the existing reference to 4.0.0.0 before installing this nuget package.

I know that installing the Framework 4.7.2 fixes the problem (I tested), but for reasons I cannot do it on 2008 R2 machines.

I have tried what is there : 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

Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

also

    <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>

and more...

But still no way to solve this problem with the PowerShell dll module only.

Thanks per advance for your answers.

Le ZVince
  • 39
  • 7
  • Do a clean build. You have intermediate OBJ files that were not update after you change Net Version. If still have issue in VS Solution Explorer delete library and add again. The version of the library in Net is not matching the version in the csproj file. – jdweng Feb 15 '23 at 15:25
  • Thanks for your answer jdweng, it was a good idea because I had not tried to remove the OBJ folders, for the deletion of the library : I have deleted the references and added them again, but unfortunately the problem is still there :( – Le ZVince Feb 16 '23 at 06:57
  • Than version in the csproj is not matching the version of dll installed on machine. Or the library needs a different build option (x86, 32 bit, 64 bit). Can't tell from error message if you are trying to use a version of dll that is not installed, or the library was built using a different build option. – jdweng Feb 16 '23 at 10:11

1 Answers1

0

There is really a bug with the Framework .Net 4.5.2 and the System.Net.Http library. I was not able to run the Powershell module of the app by trying all the tips provided on the internet, even if the GUI module was properly working (see my question).

We determined that on old Windows Server 2008 R2 we can have the minimum Framework .Net 4.6.1. So I updated all the solution to the 4.6.1 framework, I added the NuGet package System.Net.Http 4.3.4 and then all the modules of the solution work properly :)

Thanks to jdweng for his help :)

Le ZVince
  • 39
  • 7