Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

probing

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?

like image 570
Victor Haydin Avatar asked Mar 07 '11 13:03

Victor Haydin


People also ask

How the runtime Locates assemblies?

The process of locating an assembly involves the following steps: If a <codeBase> element is found in the application configuration file, the runtime checks the specified location. If a match is found, that assembly is used and no probing occurs. If the assembly is not found there, the binding request fails.

Where is .NET config file?

config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ directory. In the Machine. config file, locate the configuration setting you want to override in your Web.


2 Answers

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.

like image 157
Hans Passant Avatar answered Sep 28 '22 03:09

Hans Passant


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

like image 32
Fergus Bown Avatar answered Sep 28 '22 03:09

Fergus Bown