我的問題始於將.Net 2.0應用程序移動到.Net 4.0。我必須這樣做的原因是,Windows 8默認情況下不啓用早期的.Net版本,我的應用程序不能要求用戶啓用它。在.Net 4.0中加載動態程序集
該應用程序是一個NPAPI插件,它通過UnmanagedExports使用.Net組件。我將它設計爲一個低廉的應用程序,因此它必須駐留在用戶的LocalLow目錄中。
在我的應用程序中,我使用動態程序集加載機制在運行時加載幾個程序集。我用下面的方法來加載程序集,
MyInterface Instance;
Assembly assembly = Assembly.LoadFrom(AssemblyFile);
Type type = assembly.GetType(Identifier); // Identifier is implementing the MyInterface
Instance = Activator.CreateInstance(type) as MyInterface;
// Do something with the Instance
修改項目以.NET 4.0後,我注意到,(它的工作原理在其他地方)時二進制文件被放置在LocalLow目錄裏面的插件崩潰。我的下一步是用最少的代碼創建一個簡約的插件來找出問題。我注意到,動態裝配載有以下異常失敗,
System.IO.FileLoadException: Could not load file or assembly '<assemblyPath>' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515 (COR_E_NOTSUPPORTED)) --->
System.NotSupportedException: 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=131738 for more information.
我嘗試以下方法來創建一個單獨的域和加載組件,但沒有運氣,
- http://blogs.msdn.com/b/shawnfa/archive/2009/06/08/more-implicit-uses-of-cas-policy-loadfromremotesources.aspx
- http://www.west-wind.com/weblog/posts/2009/Jan/19/Assembly-Loading-across-AppDomains
添加配置 'loadFromRemoteSources' 也不能工作。看起來.Net組件不會加載.dll.config文件。 (可能是因爲UnmanagedExporting)的
我的問題是,
- 是否可以動態加載從LocalLow組裝?
- CLR 4.0中的新CAS策略是否也適用於LocalLow? 從我瞭解到目前爲止,它應該只會影響通過網絡加載的程序集
- 有沒有其他方法可以解決此問題?
你爲什麼在LocalLow中?這是一個特別安全的目錄,可由不可信應用程序寫入(低完整性進程)。它可能被視爲「網絡位置」,因爲它用於Internet Explorer和其他瀏覽器,但它可能只是一個MOTW的DLL。 – Mitch
我不得不切換到LocalLow的原因是因爲Internet Explorer在運行插件時阻止對所有其他路徑的訪問,特別是在我們正在寫入文件時,[請查看此鏈接](http://www.codeproject.com) /文章/ 18866/A-開發-S-生存-引導到IE-保護模式#writingfiles)。唯一的選擇是使用LocalLow。 – Isuru
恰恰是我的觀點。由於它可以寫入插件,.Net將它加載黑名單。 LocalLow用於需要由*插件編寫的文件,而不是插件本身。在另一個目錄中找到插件(想到Program Files)並將臨時數據文件寫入LocalLow。 – Mitch