Solved

Custom actions and rolling back installation on error

Posted on 2012-04-12
7
379 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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