MSI installer cancel not working
I have an MSI installer (set-up project) which actually calls a Windows Forms exe through system.diagnostic.process.
This form actually takes input from the user to restore a .bak file in sql server.
But if any exception occ开发者_开发技巧ur, I am not able to cancel the setup. Even after clicking the cancel button of installer the installation roll-back will not start.
Please suggest how to handle it.
Create new project that calls your windows forms exe and add installer class to it, or just add installer class to your windows forms exe (you'll have to change it's output type and modify it a little, so, for example, there is no Main() method, or startup object is not set, and your form is called from inside of install action)
Installer class should look like this:
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
// Do your magic here, call your form, do your thing...
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Rollback(IDictionary savedState)
{
// if something goes wrong, it's here you correct it and rollback the system to its previous state and undo what you already changed
base.Rollback(savedState);
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
// Here you undo changes when you cancel
base.Uninstall(savedState);
}
}
When you have your installer project ready, go to your setup project Custom Action and add primary output of your installer project to its install, commit, rollback and uninstall "folders".
Another usefull thing is getting the directory in which your app is being installed and passing its path to your installer class. you do this setting CustomActionData property of your custom action to
/INSTALLDIR="[TARGETDIR]\"
and in your installer class you get the directory using:
Context.Parameters["INSTALLDIR"]
EDIT: And installation will be canceled if an exception is thrown from within the Install method. As far as I know it's the only way to "cancel" your installation in the middle. You have to create an exception and throw it., for example like this:
If (SomethingWentWrong) throw new Exception("My exception description")
Of course, if the exception was thrown by something else (I mean, not willingly "created" by you) the rollback shoud also start. But if you do some custom changes, it has to be thrown from within the custom action's installer's install method.
Sorry if I was a bit too detailed but I got through my own set of troubles over this so I'll be happy if I can help someone to avoid it :)
精彩评论