[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.setup

Trouble creating a service installer that can be updated

Lothar Behrens

11/24/2010 9:10:00 AM

Hi,

I have a Windows Service (.NET 2.0) and have created an installer with
custom installer class that registers the setup routines on demand (to
avoid 'Service is available' errors while updating).

The code works fine on 32 Bit platforms, but fails on 64 Bit Windows 7
Enterprise (that was tested yet).

One thing I am missing is a final uninstall step that (with the code
below) will uninstall the service from registry.

Is there anything I am missing?

The service normally get's marked as deleted to be deleted at reboot.
How can I unmark the service to prevent deletion (in an updateable
installer)?

How do I the stuff correctly so that the code can be used on 32 Bit
and 64 Bit platforms?

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace ServiceSetup
{
[RunInstaller(true)]
public partial class ServiceInstaller : Installer
{
public ServiceInstaller()
{
bool isInstalled = false;
ServiceController[] controllers =
ServiceController.GetServices();

foreach (ServiceController ctrl in controllers)
{
if (ctrl.DisplayName == "Service")
{
isInstalled = true;
}
}

if (!isInstalled)
{
InitializeComponent();
}
else
{
this.serviceController1 = new
System.ServiceProcess.ServiceController();
this.serviceController1.ServiceName = "Service";
this.BeforeUninstall += new
System.Configuration.Install.InstallEventHandler(this.ServiceInstaller_BeforeUninstall);
this.BeforeInstall += new
System.Configuration.Install.InstallEventHandler(this.ServiceInstaller_BeforeInstall);
this.AfterInstall += new
System.Configuration.Install.InstallEventHandler(this.ServiceInstaller_AfterInstall);
}
}

private void ServiceInstaller_AfterInstall(object sender,
InstallEventArgs e)
{
serviceController1.Start();
}

private void ServiceInstaller_BeforeUninstall(object sender,
InstallEventArgs e)
{
try
{
serviceController1.Stop();

int repeat = 10;

while ((repeat-- > 0) && serviceController1.Status ==
ServiceControllerStatus.Running)
System.Threading.Thread.Sleep(1000);
}
catch
{

}
}

private void ServiceInstaller_BeforeInstall(object sender,
InstallEventArgs e)
{
try
{
serviceController1.Stop();

int repeat = 10;

while ((repeat-- > 0) && serviceController1.Status ==
ServiceControllerStatus.Running)
System.Threading.Thread.Sleep(1000);
}
catch
{

}
}

private void ServiceInstaller_AfterUninstall(object sender,
InstallEventArgs e)
{
// Does not really work.

//ServiceProcessInstaller = new
System.ServiceProcess.ServiceProcessInstaller();
//ServiceInstaller = new
System.ServiceProcess.ServiceInstaller();
//ServiceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
//ServiceProcessInstaller.Password = null;
//ServiceProcessInstaller.Username = null;
//ServiceInstaller.ServiceName = "Service";
//ServiceInstaller.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;

//ServiceProcessInstaller.Uninstall(e.SavedState);
//ServiceInstaller.Uninstall(e.SavedState);

}
}