0

I have a .NET Framework 4.6.2 application myApp.exe with several internal and external dependencies. All dependencies are included into the main application myApp.exe as resources.

On application startup, our custom assembly loader gets notified via the event AssemblyResolve and loads the requested assembly from myApp.exe's resources via GetManifestResourceStream() and Assembly.Load().

This works fine, except App.config: the resources contain myApp.App.config, but the custom assembly loader is not loading it yet. This leads to a problem because all the assembly binding redirects specified in App.config are not applied by the .NET Runtime. When placing myApp.exe.config beside myApp.exe (as usual in .NET), everything is working fine.

I'm trying to find a way to avoid the additional file myApp.exe.config. Instead, I want to load the app config (especially the binding redirects) from the resources as well. Is that even possible? If yes, how can I do so?

mu88
  • 4,156
  • 1
  • 23
  • 47
  • How did you include and load `App.config` file? – Pavel Anikhouski May 04 '20 at 08:57
  • @PavelAnikhouski What exactly do you mean? `App.config` is not loaded - that is the challenge to solve. But it is embedded as a resource. – mu88 May 04 '20 at 09:02
  • I had to reread the question. Why do you need binding redirects when you have correct dlls embedded? – Jacek May 04 '20 at 09:25
  • @Jacek: due to dependency hell: we're referencing and embedding `System.Net.Http` in a different version than in GAC. This crashes at runtime. Adding a binding redirect via `myApp.exe.config` solves the problem. – mu88 May 04 '20 at 09:26
  • I suppose you embed the correct `System.Net.Http` inside? Did you try this?https://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime – Jacek May 04 '20 at 09:33
  • We're facing exactly the issue as described in your link: since the assembly with the wrong version is found in the GAC, the `AssemblyResolve` event is not fired and therefore our custom assembly loader cannot do its work. – mu88 May 04 '20 at 10:03

1 Answers1

0

Unfortunatelly, you can't tell CLR which file you want to load as a runtime config. Runtime looks up for xml configuration in following order:

  1. application config
  2. machine config

The lookup is performed every time when CLR needs info from config, for example binding redirects and assembly locations on assembly load.

It is mostly a part of assembly loading process and it is better to leave as is. You can read more about it here: https://learn.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies

Edit: However, you can live without config file. You said you have all your assemblies embedded into exe assembly and you load everything via custom resolver. Note that your custom assembly resolver is called only when standard resolve pipeline fails to load an assembly. Take a look at this answer: https://stackoverflow.com/a/26673659/2811109

Jacek
  • 829
  • 12
  • 31