I have a .exe assembly and a set of referenced assemblies that I would like to be able to deploy updated code for. I don't need to actually change the running code, but the next time I launch the executable I would want it to pick up the updated code. Currently when I attempt to copy over the running file I get an error about the .exe being used by another process.
To summarize; how can I update the code while the .exe is in use?
It is easy to do. You can rename the file, Windows has a lock on the handle, not the directory entry for the file. Now you can just copy the update without problems. All that's left to do is to get rid of the renamed file after your app starts up again. If necessary.
I don't think this is possible. For example, when deploying asp.net applications with zero downtime, best practice is to have a load balancer so you can take down one instance, update it, then take down the other for update.
You can't update the assembly when it's in use. The best option for this type of situation is to make a small executable which does a shadow copy of your assemblies, and launches them from a new location.
This way, when the user launches the program, you can shadow copy (locally) from the deployment site, which can always be overwritten.
ClickOnce gives you some options. There is an update strategy of "update after application startup."
For more complex scenarios, there is the System.Deployment namespace. For example, you could periodically poll for updates in your application.
This class will rename the currently running executable, if it completes without exception, you can simply write the new executable, then relaunch, eg:
Ourself.Rename();
// Download or copy new version
File.Copy(newVersion, Ourself.FileName());
// Launch new version
System.Diagnostics.Process.Start(Ourself.FileName());
// Close current version
Close(); // Exit();
Easy enough?
class Ourself
{
public static string FileName() {
Assembly _objParentAssembly;
if (Assembly.GetEntryAssembly() == null)
_objParentAssembly = Assembly.GetCallingAssembly();
else
_objParentAssembly = Assembly.GetEntryAssembly();
if (_objParentAssembly.CodeBase.StartsWith("http://"))
throw new IOException("Deployed from URL");
if (File.Exists(_objParentAssembly.Location))
return _objParentAssembly.Location;
if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName))
return System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName;
if (File.Exists(Assembly.GetExecutingAssembly().Location))
return Assembly.GetExecutingAssembly().Location;
throw new IOException("Assembly not found");
}
public static bool Rename()
{
string currentName = FileName();
string newName = FileName() + ".ori";
if (File.Exists(newName))
{
File.Delete(newName);
}
File.Move(currentName, newName);
return true;
}
}
Just an idea: Try MEF and [Import["http://someRemoteResource"]]
What about the following:
The best idea would be one of the other answers already suggested... like using recomposition with MEF and or ClickOnce. However, those solutions won't help you for "this deploy". They require you to make changes to the exe and or create a boot strap executable, which will only help you for the next deploy.
For this deploy you can try doing this (I've never done this before, but theoretically it could work):
The RunOnce key contains command line commands which are Run Once on reboot, and then removed from the registry so they don't run again. This is how InstallShield allows you to overwrite certain dll's while they are in use by other applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With