none
AssemblyResolve and .resources issue RRS feed

  • Question

  • Hi,

    I have a big problem with loading an assembly from a different folder.

    Folder 1: Main Application

    Folder 2: Assembly

    The assembly has a reference to the WPFToolkit.Extended.dll. This assembly is also stored in Folder 2.

    So I'm using the AssemblyResolve event in my Main Application to search for WPFToolkit.Extended.dll after my Assembly is loaded with Assembly.Load().

    This is working. But then the AssemblyResolve event is fired again, because it is searching for "WPFToolkit.Extended.resources". This dll is not existing, so I return null. But then it crashes because of an FileNotFoundException ("WPFToolkit.Extended.resources.dll"). I know since .NET 4.0  it is always searching for the satellite assemblies, but I don't understand why it is a big problem, if it is not existing? The AssemblyResolve events is also firing for a lof of other resource DLLs which are not existing and it is always fine, if I return null, but with WPFToolkit.Extended.dll it is not working.

    I also figure out that the else-code is not running in this case, because if it is searching for the WPFToolkit.Extended.resources.dll the "RequestingAssembly" is not null, it contains the WPFToolkit.Extended assembly.

    private static bool isSatelliteAssembly(string assemblyName)
            {
                int firstComma = assemblyName.IndexOf(',');
    
                try
                {
                    if (string.Equals(assemblyName.Substring(firstComma - 10, 10), ".resources"))
                    {
                        int cultureStart = assemblyName.IndexOf("Culture=", firstComma) + 8;
                        int cultureEnd = assemblyName.IndexOf(',', cultureStart);
    
                        if (!string.Equals(assemblyName.Substring(cultureStart, cultureEnd - cultureStart), "neutral"))
                            return true;
                    }
                }
                catch
                { return false; }
    
                return false;
            }
    
    
            private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                if (args.RequestingAssembly == null)
                {
                    if (!isSatelliteAssembly(args.Name))
                    {
                        var assemblyName = new AssemblyName(args.Name);
                        string name = assemblyName.Name + ".dll";
    
                        var result = System.IO.Directory.GetFiles(App.AppsDir, name, System.IO.SearchOption.AllDirectories);
                        if (result.Length > 0)
                        {
                            try
                            {
                                manuallyLoadedAssemblies.Add(assemblyName.Name);
                                return Assembly.LoadFile(result[0]);
                            }
                            catch
                            {
                                return args.RequestingAssembly;
                            }
                        }
                    }
                    else
                    {
                        int indexOfFirstComma = args.Name.IndexOf(',');
                        string satName = args.Name.Substring(0, indexOfFirstComma);
                        satName = satName.TrimEnd(".resources".ToCharArray());
                        if (manuallyLoadedAssemblies.Contains(satName))
                        {
                            var assemblyName = new AssemblyName(args.Name);
                            string name = assemblyName.Name + ".dll";
    
                            var result = System.IO.Directory.GetFiles(App.AppsDir, name, System.IO.SearchOption.AllDirectories);
                            if (result.Length > 0)
                            {
                                try
                                {
                                    manuallyLoadedAssemblies.Add(assemblyName.Name);
                                    return Assembly.LoadFile(result[0]);
                                }
                                catch
                                {
                                    return args.RequestingAssembly;
                                }
                            }
                        }
                    }
                }
               
                return args.RequestingAssembly;
            }


    • Edited by Sascha L Monday, February 20, 2012 10:16 AM
    • Moved by Sheldon _Xiao Wednesday, February 22, 2012 5:57 AM (From:Windows Presentation Foundation (WPF))
    Monday, February 20, 2012 10:13 AM

All replies

  • If I add the following to the WPFToolkit.Extended AssemblyInfo.cs:

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

    it is working, BUT: I can't add those info to third-party DLLs!

    <label class="validity-alert" style="display:none;position:absolute;"></label><iframe src="javascript:""" style="display:none;position:absolute;z-index:-1;"></body></iframe>
    Monday, February 20, 2012 11:40 AM