解決Azure中COULD NOT LOAD FILE OR ASSEMBLY問題


當把Web項目部署到Azure的時候,有時候會出現COULD NOT LOAD FILE OR ASSEMBLY的問題。這樣問題一般是由於某些依賴的Assembly的版本不對導致的,例如在開發機上可能同時安裝了MVC 3和MVC4。那么開發機不會出現問題,因為可以找到相應的Assembly。但是部署到Azure后就出現了依賴文件的問題。例如問題如下:

"COULD NOT LOAD FILE OR ASSEMBLY 'SYSTEM.WEB.WEBPAGES, VERSION=2.0.0.0, CULTURE=NEUTRAL, PUBLICKEYTOKEN=31BF3856AD364E35' OR ONE OF ITS DEPENDENCIES. THE LOCATED ASSEMBLY'S MANIFEST DEFINITION DOES NOT MATCH THE ASSEMBLY REFERENCE. (EXCEPTION FROM HRESULT: 0X80131040)"
Could not load file or assembly 'System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. 

如果你登錄到Azure的機器,然后修改注冊表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion!EnableLog] (DWORD) to 1

你可以看到更詳細的信息,例如如下:

=== Pre-bind state information ===
LOG: User = NT AUTHORITY\NETWORK SERVICE
LOG: DisplayName = System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///E:/sitesroot/1/
LOG: Initial PrivatePath = E:\sitesroot\1\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: E:\sitesroot\1\web.config
LOG: Using host configuration file: D:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from D:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///D:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/sts/5c2051b2/ac0bf06b/System.Web.Helpers.DLL.
LOG: Attempting download of new URL file:///D:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/sts/5c2051b2/ac0bf06b/System.Web.Helpers/System.Web.Helpers.DLL.
LOG: Attempting download of new URL file:///E:/sitesroot/1/bin/System.Web.Helpers.DLL.
LOG: Attempting download of new URL file:///E:/sitesroot/1/bin/System.Web.Helpers/System.Web.Helpers.DLL.
LOG: Attempting download of new URL file:///D:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/sts/5c2051b2/ac0bf06b/System.Web.Helpers.EXE.
LOG: Attempting download of new URL file:///D:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/sts/5c2051b2/ac0bf06b/System.Web.Helpers/System.Web.Helpers.EXE.
LOG: Attempting download of new URL file:///E:/sitesroot/1/bin/System.Web.Helpers.EXE.
LOG: Attempting download of new URL file:///E:/sitesroot/1/bin/System.Web.Helpers/System.Web.Helpers.EXE.

 

解決方法查找項目的References下面各個Assembly,查找其版本

image

如果版本用錯了,那么需要重新引用正確的版本。

如果Azure上沒有安裝支持的庫或者runtime,需要把Copy Local修改成True,這樣dll就一並打包到Azure的package中。

image

 

有時候這些問題很難查,最好也是最花時間的方式是把整個包拉下來,通過ILSpy等工具查依賴項,細心仔細一個個查。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM