C# Reflection: Unable to load System.Net.Http

49 Views Asked by At

In my project (running un .NET 4.5) I'm loading in ReflectionOnly context an assembly, which one of it's dependencies is System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.

When I'm trying to load it with:

Assembly.ReflectionOnlyLoad("System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

I'm getting this exception:

Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

How can I load this type properly?

1

There are 1 best solutions below

0
On

To resolve this issue, you need to ensure that the assembly and its dependencies are available in the application's probing path.

Here are a few possible solutions:

  1. Add the assembly to the application's probing path: You can add the assembly to the application's probing path by using the Assembly.LoadFile method. For example:
Assembly.LoadFile("path_to_System.Net.Http.dll");
  1. Use a binding redirect: If the assembly is already present in the probing path but with a different version, you can use a binding redirect to redirect the application to the desired version. You can do this by adding a element to the application's configuration file (app.config):
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
        </assemblyBinding>
    </runtime> 
</configuration>
  1. Use a private assembly: If the assembly is not available in the probing path, you can load it as a private assembly. To do this, you need to use the Assembly.Load method with the LoadFrom parameter. For example:
Assembly.LoadFrom("path_to_System.Net.Http.dll");

Once you have added the assembly and its dependencies to the application's probing path, you should be able to load it using Assembly.ReflectionOnlyLoad.