Introduction
Running an application shadow copied can be useful for purposes like auto-updating. On normal execution, the assembly gets locked by the loader and can't be substituted while it's executed. On shadow copying, all assemblies referenced are copied to a cache path, and loaded/executed from this location - so the assemblies aren't locked, and can be changed.
Background
This technique is well known in ASP.NET, but on the application side, there's little information (even on MSDN). Therefore, I'd like to share my findings.
Using the Code
For executing an assembly from the cache path, we have to use a loader/bootstrapper. This little program creates a domain from which the application gets loaded. That means, when we want to start the application, we have to start the loader that loads the application for us.
The code for the application and all the referenced assemblies need no change.
The code for the loader is:
using System;
using System.IO;
namespace Loader
{
static class Program
{
[LoaderOptimization(LoaderOptimization.MultiDomainHost)]
[STAThread]
static void Main()
{
string startupPath = Path.GetDirectoryName(
System.Reflection.Assembly
.GetExecutingAssembly().Location);
string cachePath = Path.Combine(
startupPath,
"__cache");
string configFile = Path.Combine(
startupPath,
"MyApplication.exe.config");
string assembly = Path.Combine(
startupPath,
"MyApplication.exe");
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "MyApplication";
setup.ShadowCopyFiles = "true";
setup.CachePath = cachePath;
setup.ConfigurationFile = configFile;
AppDomain domain = AppDomain.CreateDomain(
"MyApplication",
AppDomain.CurrentDomain.Evidence,
setup);
domain.ExecuteAssembly(assembly);
AppDomain.Unload(domain);
Directory.Delete(cachePath, true);
}
}
}
Points of Interest
This simple program gives us the possibility to easily create auto updates (that replace the application's assembly).
History
- 07 October 2008 - Initial release
- 14 October 2008 - Article updated