2011-03-15 29 views
3

我寫了一個自定義的AssemblyResolve方法來處理exe文件以外的文件夾中的程序集。但一旦顯示缺少「Microsoft.Practices.EnterpriseLibrary.Common.resources」。雖然我有Microsoft.Practices.EnterpriseLibrary.Common.dll,我沒有Microsoft.Practices.EnterpriseLibrary.Common.resources.dll。我如何手動加載Microsoft.Practices.EnterpriseLibrary.Common.resources?AppDomain AssemblyResolve事件要求Microsoft.Practices.EnterpriseLibrary.Common.resources

protected Assembly ConfigResolveEventHandler(object sender, ResolveEventArgs args) 
     { 
      //This handler is called only when the common language runtime tries to bind to the assembly and fails. 

      //Retrieve the list of referenced assemblies in an array of AssemblyName. 
      string strTempAssmbPath = ""; 
      Assembly asm = this.GetType().Assembly; 

      var uri = new Uri(Path.GetDirectoryName(asm.CodeBase)); 


      Assembly objExecutingAssemblies = Assembly.GetExecutingAssembly(); 
      AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies(); 

      //Loop through the array of referenced assembly names. 
      if (arrReferencedAssmbNames.Any(strAssmbName => strAssmbName.Name == args.Name)) 
      { 
       strTempAssmbPath = Path.Combine(uri.LocalPath, args.Name) + ".dll"; 
      } 
      //Load the assembly from the specified path.      
      Assembly myAssembly = Assembly.LoadFrom(strTempAssmbPath); 

      //Return the loaded assembly. 
      return myAssembly; 
     } 
+0

我遇到了同樣的問題,但我不相信它實際上是一個存在的DLL。 「Microsoft.Practices.EnterpriseLibrary.Common.dll」庫中附帶有資源,可能出於某種原因無法訪問這些資源。在.NET Reflector中查看它,它列出了引用,並且未列出「Microsoft.Practices.EnterpriseLibrary.Common.resources.dll」。你有沒有運氣呢? – merthsoft 2011-04-27 22:00:05

回答

3

問題已在Microsoft Connect上討論過。

建議解決辦法: 以下行添加到AssemblyInfo.cs中:

[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.MainAssembly)] 
2

我們遇到了與AssemblyResolve事件處理同樣的問題。奇怪的是,我們只在Windows XP機器上看到這個問題。我們的應用程序本地化爲多種語言,因此我們對使用NeutralResourcesLanguageAttribute猶豫不決。我們的應用程序被編譯爲.NET v3.5版本,但仍然受着AssemblyResolve變化documented爲.NET V4.0:

重要從.NET Framework 4開始,ResolveEventHandler事件引發適用於所有組件,包括 資源組件。在較早的版本中,不會爲 資源組件提出該事件。如果操作系統已本地化,則可能會多次調用處理程序 :對於後備 鏈中的每個區域,都會執行一次。

我們解決這個問題的方法是檢查e.Name並查看它是否正在查找* .Resources.dll。如果該文件未在AppDomain或已知文件夾中找到,我們將刪除「.Resources」並查找* .dll。如果該文件存在,我們加載並返回該程序集。這解決了我們的問題。

相關問題