4

I'm hooked into the Assembly.Resolve event and loading assemblies at runtime from embedded resources. This much has been working perfectly for years without a single change to the code.

Recently, I added a new reference to a dll (external.dll) which has String Resources, and while that assembly resolves without issue, it then attempts to load strings (like NameLang or PhoneLang) from its own resources (external.resources), and is not able to find the resources. The Assembly resolver receives a request for external.resources and fails to find them.

To be clear, there are no Satellite assemblies, the resources are inside external.dll, though I understand you could recompile the DLL to put the resources as Satellite Assemblies alongside the DLL, but this isn't compiled that way. Additionally, when I use Telerik JustDecompile, I can see the resource strings (NameLang and PhoneLang), so I know they exist within the assembly.

So, I thought maybe I could work around the issue by adding a conditional fork to my Assembly ResolveEventHandler, to extract the byte array of the Resources from the owning assembly, and load it into the domain but I just get a System.BadImageFormatException, assumably because the data isn't an assembly, it's an Embedded Resource file, which makes sense.

//^^^get assembly bytes from embedded resource, works
var asm = Assembly.Load(dllBytes);
if (!resourceToFind.Contains(".resources"))
    return asm;
else
    var resourceNames = asm.GetManifestResourceNames();
    var resourceName = resourceNames.FirstOrDefault();
    var assemblyData = LoadResourceBytes(asm, resourceName);
    return Assembly.Load assemblyData;
    //^^^System.BadImageFormatException: Bad IL format. Silly me thinking it would work

So my Primary question is: How do I load the embedded String Resources from an Assembly Resolve event when it's looking for external.resources?

(and secondarily: Why isn't it able to find it's resources after the assembly has been loaded?)

turkinator
  • 905
  • 9
  • 25
  • 2
    This might be useful: https://stackoverflow.com/questions/4368201/appdomain-currentdomain-assemblyresolve-asking-for-a-appname-resources-assembl – Iain Ballard Feb 14 '18 at 14:23

1 Answers1

0

I know this is an ancient question but I ran across it when I was trying to track down a similar problem.

I think the solution for handling the '.resources' assembly load failure (which likely crops up when loading an assembly that has localizable strings) is to return null in your event handler instead of trying to resolve it at all.

Half way through the link @iain-bollard commented with it shows that solution as well.

Naylor
  • 752
  • 7
  • 20