15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

680 ❘ ChaPTer 25 windOws services<br />

The installer Class<br />

The class ProjectInstaller is derived from System.Configuration.Install.Installer. This is the<br />

base class for all custom installers. With the Installer class, it is possible to build transaction-based<br />

installations. With a transaction-based installation, it is possible to roll back to the previous state if<br />

the installation fails, <strong>and</strong> any changes made by this installation up to that point will be undone. As you<br />

can see in Figure 25-9, the Installer class has Install(), Uninstall(), Commit(), <strong>and</strong> Rollback()<br />

methods, <strong>and</strong> they are called from installation programs.<br />

The attribute [RunInstaller(true)] means that the class ProjectInstaller should be invoked when<br />

installing an assembly. Custom action installers, as well as installutil.exe (which is used later in this<br />

chapter), check for this attribute.<br />

InitializeComponent() is called inside the constructor of the ProjectInstaller class:<br />

using System.ComponentModel;<br />

using System.Configuration.Install;<br />

namespace Wrox.ProCSharp.WinServices<br />

{<br />

[RunInstaller(true)]<br />

public partial class ProjectInstaller: Installer<br />

{<br />

public ProjectInstaller()<br />

{<br />

InitializeComponent();<br />

}<br />

}<br />

}<br />

code snippet QuoteService/ ProjectInstaller.cs<br />

Process installer <strong>and</strong> service installer<br />

Within the implementation of InitializeComponent(), instances of the ServiceProcessInstaller class<br />

<strong>and</strong> the ServiceInstaller class are created. Both of these classes derive from the ComponentInstaller<br />

class, which itself derives from Installer.<br />

Classes derived from ComponentInstaller can be used with an installation process. Remember that a<br />

service process can include more than one service. The ServiceProcessInstaller class is used for the<br />

configuration of the process that defines values for all services in this process, <strong>and</strong> the ServiceInstaller<br />

class is for the configuration of the service, so one instance of ServiceInstaller is required for each<br />

service. If three services are inside the process, you need to add ServiceInstaller objects — three<br />

ServiceInstaller instances are needed in that case:<br />

partial class ProjectInstaller<br />

{<br />

/// <br />

/// Required designer variable.<br />

/// <br />

private System.ComponentModel.IContainer components = null;<br />

/// <br />

/// Required method for Designer supportdo not modify<br />

/// the contents of this method with the code editor.<br />

/// <br />

private void InitializeComponent()<br />

{<br />

this.serviceProcessInstaller1 =<br />

new System.ServiceProcess.ServiceProcessInstaller();<br />

this.serviceInstaller1 =<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!