Monday, January 27, 2014

Changing the way .net looks for dependencies

Whenever I have to clean up my machine in preparation for changing my hardware or an OS refresh, I always find some forgotten gems in the process. Here’s one that I’m throwing up onto my blog for prosperity sake.

Every so often I have a project with lots of assemblies and dependencies which results in a deployment folder that can be daunting to the uninitiated. It would be great if the deployment folder had the exe and the config file and all the other dependencies were hidden in subfolders.

Here’s an example of how to do that.

  1. First, create two projects. Shell is an exe and Dependency is a ClassLibrary
  2. Change the Build folder of Shell to point to “..\AssemblyOutput”
  3. Change the Build folder of Dependency to point to “..\AssemblyOutput\modules”
  4. In the Shell project, add a reference to the Dependency project
  5. In the Shell’s references, set the CopyLocal property of the Dependency reference to “False”. If you don’t do this, the assembly will be copied to the root folder of the Shell’s output folder.
  6. Add the following to the Shell’s app config:
<configuration>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      
      <!-- Probing path is relative to the executable. Multiple values are supported (bin;bin2;modules;modules\subfolder)-->
      <probing privatePath="modules"/>

    </assemblyBinding>
  </runtime>

</configuration>

Now when you run the project, the first time the code comes across a reference to a type inside the Dependency assembly, it will load the assembly in the modules folder.

Happy coding.

submit to reddit

0 comments: