AppDomain的AssemblyResolve事件要求Microsoft.Practices.EnterpriseLibrary.Common.resources。

3
我写了一个自定义的AssemblyResolve方法来处理位于exe文件之外的文件夹中的程序集。但是它显示缺少"Microsoft.Practices.EnterpriseLibrary.Common.resources"。虽然我有Microsoft.Practices.EnterpriseLibrary.Common.dll,但我没有Microsoft.Practices.EnterpriseLibrary.Common.resources.dll。我该如何手动加载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;  
        }

我遇到了同样的问题,但我并不确定它实际上是存在的dll。Microsoft.Practices.EnterpriseLibrary.Common.dll库附带了一些资源,也许由于某种原因无法访问这些资源。在.NET Reflector中查看它时,它列出了引用,但未列出Microsoft.Practices.EnterpriseLibrary.Common.resources.dll。你有解决这个问题的好运吗? - merthsoft
2个回答

3

问题已在Microsoft Connect上讨论。

建议的解决方案: 将以下行添加到AssemblyInfo.cs中:

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

2
我们在处理AssemblyResolve事件处理程序时遇到了同样的问题。奇怪的是,我们只在Windows XP机器上看到这个问题。我们的应用程序本地化到许多语言,因此我们不愿使用NeutralResourcesLanguageAttribute。我们的应用程序编译为.NET v3.5,但仍受到为.NET v4.0文档记录的AssemblyResolve更改的影响:
引用: 重要提示 从.NET Framework 4开始,ResolveEventHandler事件为所有程序集(包括资源程序集)引发。在早期版本中,该事件不会为资源程序集引发。如果操作系统已本地化,则可能为每个备用项链中的每个区域设置调用该处理程序多次。
我们解决这个问题的方法是检查e.Name并查看是否正在寻找*.Resources.dll。如果在AppDomain或已知文件夹中找不到该文件,则将“.Resources”删除并查找*.dll。如果该文件存在,则加载并返回该程序集。这为我们解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接