Could not load file or assembly 'System.Net.Http'

21,578

Thanks to @Jehof:

Reason

Web.config had redirect of System.Net.Http from 4.0.0 to 4.1.1, and GAC had only assembly version 4.0.0.

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <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-4.1.1.1" newVersion="4.1.1.1" />
      </dependentAssembly>
...
    </assemblyBinding>
  </runtime>


</configuration>

Solution

Removing this redirect solved the problem.

Root cause

I think, I had this assembly redirect in Web.config, because I installed System.Net.Http via nuget at some point, and then uninstalled it. However assembly redirect was not removed automatically.

Share:
21,578

Related videos on Youtube

one_mile_run
Author by

one_mile_run

Updated on August 05, 2020

Comments

  • one_mile_run
    one_mile_run almost 4 years

    1. Error when starting app

    Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
    
    Stack Trace: 
    
    
    [FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.]
       System.Web.Http.GlobalConfiguration..cctor() +0
    
    [TypeInitializationException: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception.]
       System.Web.Http.GlobalConfiguration.get_Configuration() +14
       SerilogWeb.Classic.WebApi.PreApplicationStartModule.Register() +10
    
    [InvalidOperationException: The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
       System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +614
       System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +138
       System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +164
       System.Web.Compilation.BuildManager.ExecutePreAppStart() +156
       System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +695
    
    [HttpException (0x80004005): The pre-application start initialization method Register on type SerilogWeb.Classic.WebApi.PreApplicationStartModule threw an exception with the following error message: The type initializer for 'System.Web.Http.GlobalConfiguration' threw an exception..]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +658
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +190
    

    2. Csproj

    ...
      <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    ...
      <Reference Include="System.Net.Http" />
    ...
    

    When I display System.Net.Http properties in solution it has:

    Aliases = global
    CopyLocal = False
    Path = C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll
    Resolved = True
    Runtime Version = v4.0.30319
    Specific Version = True
    Strong Name = True
    Version = 4.0.0.0
    

    3. Fusionlog

    When starting this app, there are two logs, for v4.0.0.0 and v.4.1.1.1. Is this an issue?

    For v4.0.0.0

    *** Assembly Binder Log Entry  (x) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\IIS Express\iisexpress.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
     (Fully-specified)
    LOG: Appbase = x
    LOG: Initial PrivatePath = x\bin
    LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
    LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
    LOG: AppName = 12345
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: x\web.config
    LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: GAC Lookup was unsuccessful.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
    LOG: All probing URLs attempted and failed.
    

    For v4.1.1.0

    *** Assembly Binder Log Entry  (x) ***
    
    The operation failed.
    Bind result: hr = 0x80070002. The system cannot find the file specified.
    
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\IIS Express\iisexpress.exe
    --- A detailed error log follows. 
    
    === Pre-bind state information ===
    LOG: DisplayName = System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
     (Fully-specified)
    LOG: Appbase = file:///C:/x/
    LOG: Initial PrivatePath = C:\x\bin
    LOG: Dynamic Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
    LOG: Cache Base = C:\Users\y\AppData\Local\Temp\Temporary ASP.NET Files\vs\xyz
    LOG: AppName = 12345
    Calling assembly : (Unknown).
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: C:\x\web.config
    LOG: Using host configuration file: w\Documents\IISExpress\config\aspnet.config
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Redirect found in application configuration file: 4.1.1.1 redirected to 4.1.1.1.
    LOG: ProcessorArchitecture is locked to MSIL.
    LOG: Post-policy reference: System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
    LOG: GAC Lookup was unsuccessful.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.DLL.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/Users/y/AppData/Local/Temp/Temporary ASP.NET Files/vs/xyz/12345/System.Net.Http/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http.EXE.
    LOG: Attempting download of new URL file:///C:/x/bin/System.Net.Http/System.Net.Http.EXE.
    LOG: All probing URLs attempted and failed.
    

    4. GAC

    $ gacutil -l
    
    ...
    System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
    ...
    

    Is System.Net.Http.dll in C:\Windows\Microsoft.NET\assembly... ?

    • GAC_32 - no
    • GAC_64 - no
    • GAC_MSIL - yes

    5. This is assembly first location

    C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll

    System.Net.Http.dll 197 kB (file version: 4.7.2046.0)
    

    Assembly check:

    $ sn.exe -T "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll"
    
    Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Public key token is b03f5f7f11d50a3a
    

    6. This is assembly second location

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll

    System.Net.Http.dll 84 kB (file version: 4.6.1055.0)
    

    Assembly check:

    $ sn.exe -T "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll"
    
    Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.0
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Public key token is b03f5f7f11d50a3a
    

    I think I have all information, but I can't figure out the solution. I might replace .NETFramework assembly with that from GAC or the way around, but it is blind shot - I don't know what is the reason.

    // UPDATE

    Web.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />
      </system.web>
    
      <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-4.1.1.1" newVersion="4.1.1.1" />
          </dependentAssembly>
    ...
        </assemblyBinding>
      </runtime>
    
    
    </configuration>
    
    • Jehof
      Jehof almost 7 years
      why do you have an assembly version redirect in your app.config?
    • one_mile_run
      one_mile_run almost 7 years
      wow, good point. I have not notice this, please see my update in question. I have no idea why it is there, for sure some nuget package added this.
    • Jehof
      Jehof almost 7 years
      Do you get the error when running this locally on your development machine or on a production/test server?
    • one_mile_run
      one_mile_run almost 7 years
      @Jehof: Perfect! Following your trail, I removed dependentAssembly redirect for System.Net.Http both in Web.config and in my test project in app.config. And it works! Feel free to write an answer, I will mark it as the solution.
    • laika
      laika almost 7 years
      @one_mile_run Check this answer regarding assembly redirects. Furthermore it would be good if you check which package has caused this dependency. BTW, System.Net.Http assemblies of version 4.1 and higher are no longer located in GAC, they are distributed out-of-band.
    • one_mile_run
      one_mile_run almost 7 years
      @laika: Very useful information. Is there any dependency checker/viewer for nuget packages?
    • one_mile_run
      one_mile_run almost 7 years
      @Jehof: I think, I had this assembly redirect in Web.config, because for this project I installed System.Net.Http via nuget at some point, and then uninstalled it. However assembly redirect was not removed automatically.