8
votes

I am building a .Net standard library, which builds fine but on testing, I get this error

HResult=-2147024894 Message=Could not load file or assembly 'System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Source=Library Trial

I have installed System.Net.Http Nuget Package still no success. It's a fresh project so what must I be doing wrong

4
Please provide a minimal reproducible example. We don't know what your project looks like, what the project type is, or how you're testing it. Versioning can be very tricky - the details all matter.Jon Skeet
Some where in your project is trying to reference the version 4.1.1.1 which is different from the one which is loaded through Nuget. Try searching in packages.config file. and change the version name to the one which you are referencing through Nuget.Ved Prakash Tiwari
Thanks, I just gave up and used the new .Net standard 2.0 on VS 2017. And it just works. Thanks Jon, I was using Net standard 1.6 on VS 2015, didn't do anything special, just create a fresh project and reference system.net.http namespace. Thanksokeziestanley
@JonSkeet: The MCVE is trivial to make. Using VS2017 (15.3.3), create a new C# .NET Standard library, with the .NET Framework version at the top set to 4.6.1 (will default to .NETStandard v1.4). Add a static function to it that calls new System.Net.Http.HttpClient();. Create a second C# project in the same solution, this one a desktop console app. Reference the .NETStandard library. In the console app's Main function, call the library's static function. Run as Debug, changing nothing. Behold an exception. It took me longer to write this than it did to repro the issue.CBHacking

4 Answers

11
votes

As Ved Tiwari said above, remove the reference to "4.1.1.1" in your app.config (or solution/project).

For example, I removed the below and it started working again:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" />
</dependentAssembly>
1
votes

To fix that same error within my solution, not only did I remove the bindingRedirect for System.Net.Http from Web.config (see @Rado's answer) but also needed to remove Version, Culture, etc. and HintPath from it's reference in the project file (*.csproj).

Essentially, changed in *.csproj from

<Reference Include="System.Net.Http, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
  <HintPath>..\packages\System.Net.Http.4.1.0\lib\net46\System.Net.Http.dll</HintPath>
</Reference>

to

<Reference Include="System.Net.Http"/>
0
votes

If you installed the System.Diagnostics.DiagnosticSource dependency, remove it and update System.Net.Http to version 4.1.1.1

-1
votes

Add following in your web.config file:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.0.0.0" />
</dependentAssembly>