Monday, January 08, 2007

Some tips about creating an installer

I am working on custom installer for deploying DTSX packages. It also includes few custom actions like creating databases, populating data, attaching dtsConfig files, etc...

Few Golden rules:
1. “Uninstall/Remove/Delete all resources during uninstall and rollback, you install using custom action”.
2. Suppress all exceptions in Rollback.
3. Validate “ALL” parameters supplied by user.
4. Create a separate file that stores all strings used by Installer for user interaction.
5. Always have a log file.
6. Not recommended: You can use MessageBox to debug your installer.

How to implement:
1. Uninstall:
Create a text file “uninstall.log”. This should ideally have list of all resources that has been installed using custom install method. It should also have additional parameters (eg username, password) that will be useful for uninstalling or removing the resources.
Rollback:
Create a flag that states whether a resources has been deployed or not. Add all parameters required in state server:
Eg: stateSaver.Add("someParameter", ParameterValue); -- in Install method
if(savedState["someParameter"] != null) // Required
string someParameterInRollback = savedState["someParameter"].ToString(); -- in Rollback method

2. Keep a general try / catch loop. This will allow you to do maximum damage control J in rollback phase.

public override void Rollback(System.Collections.IDictionary savedState)
{
base.Rollback(savedState);
try
{
//
//Rollback Action
//
}
catch (Exception exception)
{
//Suppress all exceptions
}
}

3. Keep a separate method for this functionality say “private void ValidateUserParameters(System.Collections.IDictionary stateSaver)”. It should check:
a. Whether parameter is entered or not -Context.Parameters.ContainsKey("SOMEPARAMETER")
b. Whether entered parameter is null or empty - String.IsNullOrEmpty(Context.Parameters["SOMEPARAMETER"])
c. Whether entered parameter is in valid range
Always report “all” invalid parameters. i.e. have some flag that signifies invalid parameters and an errorMessage string that you can append when you get an invalid parameters and display at end.

4. This will helpful in change management. Eg:
public const string InstallerUsage = "Usage: msiexec /i UrMSI.msi SOMEPARAMETER=\"Some Paramter Name\" ";
5. Log file will help user know whether Installer was successful or not. Also, it can help him trace the action done by installer. You can have a text log file or you can log it in application log (use System.Diagnostics.EventLog.WriteEntry()).

6. Add reference to "System.Window.Forms” and you can use all windows based forms. But remember to remove the reference while Release.

No comments: