今天遇到一个奇怪问题,有一个ASP.NET项目,访问时总是显示不能加载EF4.1的错误(项目中对EF的引用都已升级至EF5.0):
Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
源错误:
执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
程序集加载跟踪: 下列信息有助于确定程序集“EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”未能加载的原因。
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
问题怪就怪在我找遍整个项目的代码,也没找到哪个地方引用了Entity Framework 4.1.0.0。
后来,注意到错误信息中的提示——“dTo enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1”,想到是不是可以通过日志找出究竟哪个地方引用了EF 4.1。进入注册表一看,有HKLM\Software\Microsoft\Fusion,但没有HKLM\Software\Microsoft\Fusion!EnableLog。不知该如何下手?
在网上搜了搜,在stackoverflow上发现How to enable assembly bind failure logging (Fusion) in .NET的回答:
Add the following values to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion
Add:
DWORD ForceLog set value to 1
DWORD LogFailures set value to 1
DWORD LogResourceBinds set value to 1
String LogPath set value to folder for logs ie) C:\FusionLog\
于是在注册表进行了这样的设置:
然后再访问,在页面显示的错误信息中包含了下面一行:
Calling assembly : Devtalk.EF.CodeFirst, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
在C:\FusionLog\Default\64baea7c中也可以找到对应的日志文件EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.HTM
于是,真相大白——这个项目中引用了Devtalk.EF.CodeFirst.dll,而它引用了Entity Framework 4.1,从而引起这个问题。
更新:
如果不想生成日志文件,只想在错误信息中显示“Calling assembly”,只需要在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion中将LogResourceBinds的值设置为1(DWORD(32-bit))。
注:修改注册表之后需要重启IIS才能生效