Solved

Custom actions and rolling back installation on error

Posted on 2012-04-12
7
378 Views
Last Modified: 2012-04-13
I have a Setup project and I added installer class to it that performs couple of actions via custom functions I can from Install procedure in my Install class I added to custom actions.

I need help writing Rollback procedure to reverse installation in case on an error. Can someone help by pointing me in the right direction?
0
Comment
Question by:YZlat
  • 5
  • 2
7 Comments
 
LVL 40
ID: 37838523
Override the Rollback method in your class, and undo there any operation that was done by the installer, using a lot of checks and Try...Catch. If the installation creates a file for instance, make sure that it exists before trying to delete it in the Rollback method.
0
 
LVL 35

Author Comment

by:YZlat
ID: 37839042
How would I activate the Rollback method in case of an error?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 37839311
Simply throw an InstallException.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 35

Author Comment

by:YZlat
ID: 37839783
Thanks, I will try that
0
 
LVL 35

Author Comment

by:YZlat
ID: 37843979
I added custom action for the rollback and then the following code to my Installer class but when an error occurs during installation, it does not rollback nor enters error handler in Install procedure.
Here is my code:

 public void CreateRebootSchedTask()
        {
            try
            {
                // Get the service on the local machine
                using (TaskService ts = new TaskService())
                {
                    // Create a new task definition and assign properties
                    TaskDefinition td = ts.NewTask();

                    td.RegistrationInfo.Description = "Schedules Reboot Service";

                    // Create a trigger that will fire the task at this time every other day
                    TimeTrigger tt = new TimeTrigger();

                    tt.Enabled = true;
                    tt.StartBoundary = DateTime.Now + TimeSpan.FromMinutes(5);
                    tt.Repetition.Interval = TimeSpan.FromHours(1);
                    MessageBox.Show("OS Version = " + Environment.OSVersion.Version.Major.ToString());
                    if (Environment.OSVersion.Version.Major < 6)
                    {
                        tt.Repetition.Duration = TimeSpan.FromDays(365);
                    }
                    td.Triggers.Add(tt);

                    td.Principal.LogonType = TaskLogonType.InteractiveToken;
                    td.Principal.UserId = "NT AUTHORITY\\SYSTEM";
                   // if (Environment.OSVersion.Version.Major > 6)
                   // { error will occur here
                        td.Principal.RunLevel = TaskRunLevel.Highest;
                   // }

                    // Create an action that will launch RebootTimerService whenever the trigger fires
                    td.Actions.Add(new ExecAction("MyReboot.exe", null, Environment.GetEnvironmentVariable("windir")));

                    // Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.Create, "SYSTEM", null, TaskLogonType.ServiceAccount, null);
                }
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("MyLog", "Error during installation in task creation: " + ex.Message + " - " + ex.StackTrace);
                throw new InstallException("Error during installation.", ex);
            }

        }

public override void Install(IDictionary stateSaver) 
        { 
            
            
            try
            {
                base.Install(stateSaver);
                
                CreateRebootSchedTask();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("MyLog", "Error during installation: " + ex.Message + " - " + ex.StackTrace);
                throw new InstallException("Error during installation.",ex);
            }
        }

       

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
            EventLog.WriteEntry("MyLog", "Rollback...");

            try
            {
                DeleteScheduledTask(taskName);
                RemoveDataFromDB(taskName);
            }
            catch (IOException ioex)
            {
                EventLog.WriteEntry("MyLog", "Scheduled task does not exist so it cannot be deleted: " + ioex.Message);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("MyLog", "Error during rollback: " + ex.Message + " - " + ex.StackTrace);
            }

        }

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
ID: 37844330
Figured it out! Thanks!
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 37844334
Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery/JSON passing value from one function to another 12 38
Problem!!! 8 33
How come this regular expression allows "e"? 14 38
MailAddress in vb 4 15
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question