Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Deployment  »  Installer Class and Custom Actions  »  Page 3
Installer Class and Custom Actions
by Arnaldo Sandoval | Published  10/15/2007 | Deployment | Rating:
Page 3
USING THESE EVENTS
  • You basically override the event you want to use by typing public override and space, then intellisense kicks in. 

     
  • Once you enter the event, its code should look like this:
Code:

public override void Commit(System.Collections.IDictionary savedState) 

   base.Commit(savedState);
}


  • The declaration for the Install event is a little bit different, as shown:

    Code:

    public override void Install(System.Collections.IDictionary stateSaver) 
    { 
       base.Install(stateSaver); 
    }



    Its IDictionary parameter is named differently to the names used by the other events, here it is named stateSaver; It is very important to declare and use this event if you are saving installation parameters to be shared across all the installation events. For example, suppose that you want to pass the installation's target directory stored in the [TARGETDIR] parameter. You can do that with the code below in the Install event:

Code:


stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());


The complete Install event code including the line of code above should look like this:

Code:

public override void Install(System.Collections.IDictionary stateSaver) 
{ 
   base.Install(stateSaver); 
   stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString()); 
}


Now, let's explain this statement: the stateSaver object-variable (IDictionary type) is the parameter received by the Install event, while the Context.Parameters["DP_TargetDir"] come from the CustomActionData property defined at the Deployment Project's Install custom action, as shown below:



We named it, DP_TargetDir prefixed with the forward slash (/), We get its value from the deployment project's propery [TARGETDIR], notice that we are enclosing it between double quotes, and we added a backward slash (\) before closing the double quote; You must always add the backward slash before the final double quote for this parameter, otherwise you will get an unexpected behaviour at installation time.

The deployment project at installation time will add the CustomActionData parameters to the custom action's Context Parameters using the names chosen by you (DP_TargetDir in this example).

Whe you add an entry to the stateSaver dictionary, the installation process will create a file at the installation target directory with the base name of your Installer Class and the type InstallState, based on the samples names we are using in this explanation, the InstallState name will be:

Code:

MyInstallerClassDll.InstallState


Now, the savedState parameter passed to any event of your Installation Class will have the TargetDir as one of its entries.

  • If we want to prove the explanation given in the previous section regarding the usage of the stateSaver and savedState dictionary parameters, we may use the Commit event to do so, the code below will dump all the key-values found in its savedState parameters as well as the class' Context Parameter dictionary.

    Code:

    public override void Commit(System.Collections.IDictionary savedState) 

       base.Commit(savedState); 

       StreamWriter sw = new StreamWriter("C:\\Temp\\VbCity_caic_Commit.txt", false); 

       sw.WriteLine("savedState count : " + savedState.Count.ToString()); 
       sw.WriteLine("savedState keys : " + savedState.Keys.Count.ToString()); 
       sw.WriteLine("savedState values : " + savedState.Values.Count.ToString()); 

       foreach (string k in savedState.Keys) 
       { 
          sw.WriteLine("savedState key[" + k + "]= " + savedState[k].ToString()); 
       } 

       writeContext(sw); 

       sw.Flush(); 
       sw.Close(); 

    }



    A "using System.IO;" line of code was added at the top of the class for the code in the Commit event to work because it is using the StreamWriter.

    The commit event is writing to a file on the Temp folder all the values in the savedState dictionary object as well as the Context.Parameters dictionary, the lattest by the writeContext() method (its code shown below)

    Code:

    private void writeContext(StreamWriter wrkSW) 
    { 
       wrkSW.WriteLine("Context Parameters"); 
       wrkSW.WriteLine("Count : " + Context.Parameters.Count.ToString()); 
       wrkSW.WriteLine("Keys : " + Context.Parameters.Keys.Count.ToString()); 
       wrkSW.WriteLine("Values : " + Context.Parameters.Values.Count.ToString()); 

       foreach (string k in Context.Parameters.Keys) 
       { 
          wrkSW.WriteLine("ContextKey [" + k + "]=" + Context.Parameters[k].ToString()); 
       } 
    }



    When we run the deployment project with the Commit event code shown above, the text file (VbCity_caic_Commit.txt) contains the following data

 

Sponsored Links