Change assembly search path within MSBuild

Today I’ve try to create a simple MSBuild task. It should load all NHibernate mappings, use the configuration to build the session factory and write all exceptions as errors to MSBuild build log ( and in Visual Studio output window ). Sounds pretty simple?….it isn’t 🙂

First i have to change the assembly search path to allow NHibernate to find the in mapping referenced assembly’s.

To do this, my first idea was to create a new appdomain, it isn’t possible to change the assembly search path of a running appdomain ( this where my msbuild task is running in ).  This works when you only running your MsBuild tasks from command line…but not in Visual Studio. All custom tasks there running in partial trust environment and you can no create appdomain’s within it.

After a long while of testing, i gave it up and move to “ToolTask”. ToolTask allow you to run another process as tool. This process is not running in partial trust, so you can create appdomain’s there.

This works, but you have to do all the interprocess communication stuff. Ive create input and output temp file where i serialize my parameters only as simple framework types ( when you use your custom types, you have to deal with the changed assembly search path ) and pass the filenames as argument to the tool process.