简体   繁体   中英

AppDomain AssemblyResolve Event asks for Microsoft.Practices.EnterpriseLibrary.Common.resources

I wrote a custom AssemblyResolve method to handle assembly in a folder other than exe file. But once it shows missing "Microsoft.Practices.EnterpriseLibrary.Common.resources". While I have Microsoft.Practices.EnterpriseLibrary.Common.dll, I don't have Microsoft.Practices.EnterpriseLibrary.Common.resources.dll. How do i manually load 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;  
        }

Issue has been discussed on Microsoft Connect .

Proposed solution: Add the following line to AssemblyInfo.cs:

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

We ran into this same problem with an AssemblyResolve event handler. Oddly, we only saw the issue on Windows XP machines. Our application is localized to many languages, so we were hesitant to use the NeutralResourcesLanguageAttribute. Our application was compiled for .NET v3.5, but was still being affected by the AssemblyResolve change documented for .NET v4.0:

Important Beginning with the .NET Framework 4, the ResolveEventHandler event is raised for all assemblies, including resource assemblies. In earlier versions, the event was not raised for resource assemblies. If the operating system is localized, the handler might be called multiple times: once for each culture in the fallback chain.

The way we resolved this was to check e.Name and see if it was looking for *.Resources.dll. If that file was not found in the AppDomain or known folder, we would remove ".Resources" and look for *.dll. If that file exists, we load and return that assembly. This resolved the problem for us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM