My Tutorials: C# .Net Assembly Dynamic Reference Custom Paths

Thursday, 25 October 2012

C# .Net Assembly Dynamic Reference Custom Paths


This is a bit of a how-to if you ever want to add a dll dynamically at runtime and it references other dll’s that are not in the GAC or the Application base directory.

NET assembly references are resolved in the following order:

  1. Search GAC for assembly reference.
  2. Search application directory.
  3. Search the paths specified in your app.config file using the following xml-tag:
    <probing privatePath="path 1; path 2; path 3" />


For instance say you are building a module for an application and it has a reference to some Third party dll that is not installed, just copied. Say now instead of adding the third party dll to the application base directory you want to create a bit of order and have a third party folder that is where you are going to place said dll’s.

Now typically when loading your assembly dynamically if the system can’t find the third party dll’s anywhere, it will have a heart attack. But you’re able to create assembly probing directories that gives the system a heads up as to where you plan on keeping all unregistered dll’s…

This is how you do it from a practical point of view.

In the app.config file you add this code

<configuration>

  <runtime>
    <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatepath="Plugins">
    </probing></assemblybinding>
  </runtime>

</configuration>


Above shows how I create a reference to my Plugins folder that is in the application base directory. This allows me to search for any plugin that implements my specific plugin interface. I can now also keep all my plugins separate from my actual app directory which allows me to maintain order and structure. If you wanted to add another directory, you just separate the directories with a ‘;’

<probing privatePath="Plugins; Third party; c:\lib" />

May solve a world of trouble one day…

No comments:

Post a Comment

Other Blogs and Sites

Social Media