Tuesday, December 30, 2008

Loading Satellite Assemblies From Custom Path

One thing that distracted me in default localization technologies in .NET was that  culture-specific folders are placed on one level with executable file. So if binary files lie in the root of application folder, as a result en-US, ru-RU and other folders are mixed with something like Data, Resources and others. Not good layout of folders.

By default .NET searchs for dependendent assemblies in directory of executable file in and in GAC. But developer can specify additional directories in exe.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="./Lib" />
        </assemblyBinding>
    </runtime>
</configuration>

There are some limitations: no absolute paths and no walking up the directory tree ( ..\Lib will not work). Recently I've tested this for satellite assemblies and I have found that frameowork searchs in provided directories for satellite assemblies. So I can make something like that:

Main.exe
Main.exe.config
I18N\en-US\Main.resources.dll
I18N\ru-RU\Main.resources.dll

However as I've mentioned if I move Main.exe to Bin directory this will not work correctly. I know 2 possible ways to override this:
  1. codeBase element in config file, but it requires absolute path
  2. Adding handler for ApplicationDomain.AssemblyResolve event which will return whatever you want.

No comments: