Is it possible to set assembly probing path w/o app.config?
Asked Answered
I

2

13

I need to place DLLs for my application inside subfolder. It is possible to set this subfolder via app.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="Libs"/>
    </assemblyBinding>
  </runtime>

But for some reasons I don't want to use .config file in this case. Is it possible to set probing path directly from application code? I am sure that DLLs always be within this folder.

Any ideas?

Irradiate answered 7/3, 2011 at 13:36 Comment(0)
G
14

The probing path is defined by the AppDomainSetup for the primary app domain. In the default CLR host, that AD gets automatically created before your code starts running. The only way to configure its setup is to use a .config file, it must have the same name as the exe. After which it is frozen, any changes you make in your code won't have an effect.

Workarounds are to create your own AD so you can change its setup or implementing the AppDomain.AssemblyResolve event. Neither of which compares favorably to the simple solutions: a .config file or just keeping the assembly in the right directory. Ymmv.

Grandson answered 7/3, 2011 at 15:17 Comment(1)
@Legends don't you have a web.config?Fabled
D
5

You could just subscribe to AppDomain.CurrentDomain.AssemblyResolve and check your specific location in your handler...

Donella answered 7/3, 2011 at 13:44 Comment(2)
It seems to be slightly overcomplicated solution. I don't need to resolve assemblies manually, its enough to use standard .Net probing for my subfolder.Irradiate
You can call AppDomain.CurrentDomain.AppendPrivatePath("libs") and that will work, but the method is deprecated so probably not a good idea. To use the none deprecated equivalent appears to require making a new AppDomain, which I guess you won't want to do...Donella

© 2022 - 2024 — McMap. All rights reserved.