** Jeśli jest to konieczne do wykonania przy użyciu tylko konfigurację, należy wykonać:
To mogą być obsługiwane przez wyraźnej realizacji usuwania istniejących usług (odinstalować) i następnie pozwalając nowsza wersja do zainstalowania. W tym celu należy zaktualizować ProjectInstaller.Designer.cs jak poniżej:
Należy rozważyć dodanie następującej linii na początku InitializeComponent(), która wyzwala zdarzenie do odinstalowania istniejącej usługi, zanim bieżący instalator spróbuje ponownie zainstalować tę usługę . Tutaj odinstalowujemy usługę, jeśli już istnieje.
Add następujących nazw:
using System.Collections.Generic;
using System.ServiceProcess;
Dodaj poniżej linii kodu, jak opisano wcześniej:
this.BeforeInstall += new
System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);
przykład:
private void InitializeComponent()
{
this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "This is my service name description";
this.serviceInstaller1.ServiceName = "MyServiceName";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[]{
this.serviceProcessInstaller1,
this.serviceInstaller1
}
);
}
poniżej kod wywoływane przez wypadku Odinstaluj usługa, jeśli istnieje.
void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());
foreach (ServiceController s in services)
{
if (s.ServiceName == this.serviceInstaller1.ServiceName)
{
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "MyServiceName";
ServiceInstallerObj.Uninstall(null);
break;
}
}
}
PS: Wraz z powyższymi zmianami, również należy rozważyć aktualizację wersji konfiguracji, ProductCode (i optionall UpgradeCode) dla dobrych praktyk, lepsze zarządzanie wersji, śledzenie i konserwacji
Dla jasności istnieje nazwa usługi i wyświetlana nazwa usługi. Czasami ludzie zapominają, że kolumna "nazwa" w services.msc jest nazwą wyświetlaną usługi. Nazwę usługi otrzymasz klikając prawym przyciskiem myszy na swoje usługi i przechodząc do właściwości. – Tung
@ Tung Sprawdziłem to i obie są takie same. –
Migracja do http://www.superuser.com? To naprawdę nie jest pytanie programistyczne. – CodingWithSpike