Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDomain.CurrentDomain.AssemblyResolve asking for a <AppName>.resources assembly?

using the code How to embed a satellite assembly into the EXE file provided by csharptest.net, I've created a custom assembly resolver and embedded my assemblies in my resources.

I can successfully resolve my assemblies used in but somehow AppDomain.CurrentDomain.AssemblyResolve asks for an assembly called 'AppName.resources' specifically "MyProgram.resources, Version=0.15.3992.31638, Culture=en-US, PublicKeyToken=null" which i don't know how to resolve?

I've tried to disable loading my custom assemblies from resources (placed all my assembly dll's in program directory) and just enabled AppDomain.CurrentDomain.AssemblyResolve, but it was still asking for it.

I'm a bit confused about this, will appreciate a lot if you can help me on this.

Here's my code for interested ones;

static Assembly ResolveAssemblies(object sender, ResolveEventArgs args) {     Assembly assembly = null;     string name = args.Name.Substring(0, args.Name.IndexOf(','));     if (name == "MyProgram.resources") return null;     else name = string.Format("MyProgram.Resources.Assemblies.{0}.dll", name);      lock (_loadedAssemblies)     {         if (!_loadedAssemblies.TryGetValue(name, out assembly))         {             using (Stream io = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))             {                 if (io == null)                 {                     MessageBox.Show("MyProgram can not load one of it's dependencies. Please re-install the program", string.Format("Missing Assembly: {0}", name), MessageBoxButtons.OK, MessageBoxIcon.Error);                     Environment.Exit(-1);                 }                 using (BinaryReader binaryReader = new BinaryReader(io))                 {                     assembly = Assembly.Load(binaryReader.ReadBytes((int)io.Length));                     _loadedAssemblies.Add(name, assembly);                 }             }         }     }      return assembly; } 
like image 780
HuseyinUslu Avatar asked Dec 06 '10 15:12

HuseyinUslu


1 Answers

Answering on my own;

Adding this line to AssemblyInfo.cs solves it and resolver will not get asked for resources any-more.

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

Though this is a work-around should be carefully considered multi-language applications.

More Info:

  • https://connect.microsoft.com/VisualStudio/feedback/details/526836/wpf-appdomain-assemblyresolve-being-called-when-it-shouldnt
  • http://blogs.msdn.com/b/kimhamil/archive/2008/11/11/what-does-the-neutralresourceslanguageattribute-do.aspx
  • http://forums.devshed.com/net-development-87/c-wpf-appdomain-assemblyresolve-being-called-when-it-shouldn-t-669567.html
  • http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx

This approach fails for machines with non en-US cultures. A better approach is ignoring resources on assembly resolver;

public Assembly Resolver(object sender, ResolveEventArgs args)         {             lock (this)             {                 Assembly assembly;                 AssemblyName askedAssembly = new AssemblyName(args.Name);                  string[] fields = args.Name.Split(',');                 string name = fields[0];                 string culture = fields[2];                 // failing to ignore queries for satellite resource assemblies or using [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]                  // in AssemblyInfo.cs will crash the program on non en-US based system cultures.                 if (name.EndsWith(".resources") && !culture.EndsWith("neutral")) return null;                  /* the actual assembly resolver */                 ...             }       } 
like image 200
HuseyinUslu Avatar answered Sep 23 '22 12:09

HuseyinUslu