Solved

Custom actions and rolling back installation on error

Posted on 2012-04-12
7
377 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
Simply throw an InstallException.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Author Comment

by:YZlat
Comment Utility
Thanks, I will try that
0
 
LVL 35

Author Comment

by:YZlat
Comment Utility
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
Comment Utility
Figured it out! Thanks!
0
 
LVL 35

Author Closing Comment

by:YZlat
Comment Utility
Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now