加載網絡映射盤中的assembly失敗的處理辦法


錯誤症狀:

1.{"未能加載文件或程序集“file://*****”或它的某一個依賴項。不支持操作。 (異常來自 HRESULT:0x80131515)":"file://****"};

2.{"嘗試從一個網絡位置加載程序集,在早期版本的 .NET Framework 中,這會導致對該程序集進行沙盒處理。此發行版的 .NET Framework 默認情況下不啟用 CAS 策略,因此,此加載可能會很危險。如果此加載不是要對程序集進行沙盒處理,請啟用 loadFromRemoteSources 開關。有關詳細信息,請參見 http://go.microsoft.com/fwlink/?LinkId=155569。"}

解決辦法入下文:

我有一個網絡映射盤,盤符是Z:。在Z盤下面,放了一個assembly,名為test.dll。然后,我在VS2010中建立了一個.NET 4.0的工程,程序中有下面一段代碼:

 

string dll = @"Z:\test.dll";

Assembly a = Assembly.LoadFrom(dll);

執行代碼,拋出FileLoadException異常:Could not load file or assembly 'file:///Y:\bb\nvo_cas.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)。

 

深入到內部異常:An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.。

看來是.NET的安全機制阻止加載一個網絡上(本地網或者互聯網)的assembly。

 

【辦法一】

根據提示,找到了MSDN的一篇文章。文章給出的解決辦法是:在程序的配置文件中加入下面的xml片段:

<configuration>

  ......

  <runtime>

    <!-- WARNING: will load assemblies from remote locations as fully trusted! -->

    <loadFromRemoteSources enabled="true" />

  </runtime>

  ......

</configuration>

 

按照這個解決辦法,程序運行正確。

MSDN鏈接:http://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx

 

【辦法二】

Assembly類有一個靜態函數UnsafeLoadFrom,這個函數在加載一個assembly的時候,不會進行一些安全檢查。將原先的代碼修改如下即可:

 

string dll = @"Z:\test.dll";

Assembly a = Assembly.UnsafeLoadFrom(dll);

【辦法三】

Assembly的load方法有很多的重載,可以使用其中的一個參數為byte[]的load函數。代碼如下:

 

string dll = @"Z:\test.dll";  byte[] assemblyBuffer = File.ReadAllBytes(dll);  Assembly a = Assembly.Load(assemblyBuffer);   轉載自:http://blog.csdn.net/studying/article/details/6663627  


免責聲明!

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



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