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

时间:2023-03-09 14:54:15
Could not load file or assembly 'System.Web.Http

https://github.com/awaescher/Fusion

A modern alternative to the Microsoft Assembly Binding Log Viewer (FUSLOGVW.exe)

https://github.com/awaescher/Fusion/releases/

使用FusLogVw

https://*.com/questions/4469929/could-not-load-file-or-assembly-or-one-of-its-dependencies?page=1&tab=votes#tab-top

You can use FusLogVw to find out who is loading the old assemblies, just define a path for the log, and run your solution, then check (in FusLogvw) the first line where the Unity assembly is loaded, double click it and see the calling assembly, and here you go.

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\Developer Command Prompt for VS 2017 (2)

如何使用

Fuslogvw.exe (Assembly Binding Log Viewer)

https://*.com/questions/255669/how-to-enable-assembly-bind-failure-logging-fusion-in-net/3256753#3256753

I usually use the Fusion Log Viewer (Fuslogvw.exe from a Visual Studio command prompt or Fusion Log Viewer from the start menu) - my standard setup is:

  • Open Fusion Log Viewer as administrator
  • Click settings
  • Check the Enable custom log path checkbox
  • Enter the location you want logs to get written to, for example, c:\FusionLogs (Important: make sure that you have actually created this folder in the file system.)
  • Make sure that the right level of logging is on (I sometimes just select Log all binds to disk just to make sure things are working right)
  • Click OK
  • Set the log location option to Custom

Remember to turn of logging off once you're done!

(I just posted this on a similar question - I think it's relevant here too.)

实例

在页面中搜索application对应的编码,发现存在临时文件,需要将这个文件夹删除C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lisa_60_dev_cmsweb11\931fe700\6226aa26

将application下面所有的文件夹以及文件删除,然后使用visual studio重新编译项目

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

*** Assembly Binder Log Entry  (2018/10/31 @ 13:44:32) ***

The operation failed.
Bind result: hr = 0x80131040. No description available. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable c:\windows\system32\inetsrv\w3wp.exe
--- A detailed error log follows. === Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Net.Http.Formatting | Domain ID: 4
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/
LOG: Initial PrivatePath = C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lisa_60_dev_cmsweb11\931fe700
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lisa_60_dev_cmsweb11\931fe700
LOG: AppName = 6226aa26
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/bin/System.Net.Http.Formatting.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin\System.Net.Http.Formatting.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 5.2.3.0 redirected to 5.2.6.0.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=5.2.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: GAC Lookup was unsuccessful.
LOG: The post-policy assembly reference requires probing again.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/bin/System.Net.Http.Formatting.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin\System.Net.Http.Formatting.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. *** Assembly Binder Log Entry (2018/10/31 @ 13:44:32) *** The operation failed.
Bind result: hr = 0x80131040. No description available. Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable c:\windows\system32\inetsrv\w3wp.exe
--- A detailed error log follows. === Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Net.Http.Formatting | Domain ID: 4
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/
LOG: Initial PrivatePath = C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin
LOG: Dynamic Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lisa_60_dev_cmsweb11\931fe700
LOG: Cache Base = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\lisa_60_dev_cmsweb11\931fe700
LOG: AppName = 6226aa26
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/bin/System.Net.Http.Formatting.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin\System.Net.Http.Formatting.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 5.2.3.0 redirected to 5.2.6.0.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=5.2.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: GAC Lookup was unsuccessful.
LOG: The post-policy assembly reference requires probing again.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/lisa_60_dev_cmsweb11/931fe700/6226aa26/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/Users/clu/source/repos/Edenred/LISA_6.0.0.0/Kentico11/LISA.Kentico11/bin/System.Net.Http.Formatting.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\Users\clu\source\repos\Edenred\LISA_6.0.0.0\Kentico11\LISA.Kentico11\bin\System.Net.Http.Formatting.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
WRN: Comparing the assembly name resulted in the mismatch: Build Number
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

如何关闭

Make sure you are running fuslogvw not only as admin but also from the correct Windows SDK path the Visual Studio project throwing the exception is using. Check its csproj and search for SDK within (my sdk node is named TargetFrameworkSDKToolsDirectory). Using a non matching fuslogvw version seems to not catch the exceptions (which makes sense...)

You can run this Powershell script as administrator to enable FL:

Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name ForceLog         -Value 1               -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogFailures -Value 1 -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogResourceBinds -Value 1 -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogPath -Value 'C:\FusionLog\' -Type String
mkdir C:\FusionLog -Force

and this one to disable:

Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name ForceLog
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogFailures
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogResourceBinds
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogPath

https://indexoutofrange.com/Could_not_load_file_or_assembly_or_one_of_its_dependencies/

这里提供了直接修改注册表的

第二次使用

刚开始启用,发现完全不工作。各种折腾,不知道为啥突然恢复了,可能和执行iisreset有关。也可能是修改注册表导致可用,https://*.com/a/1527249

file:///C:/FusionLogs/Default/214332ee/ICSharpCode.SharpZipLib,%20Version=0.85.3.365,%20Culture=neutral,%20PublicKeyToken=1b03e6acf1164f73.htm

分析一下发,发现是因为站点只会加载一次,除非你修改web.config或者替换dll文件,才会再次自动启动。为了测试方便,直接用iisreset来确保站点会被重新加载。