Scenario: My WCF service is hosted as a Windows service, as shown in
(Service Right Button-"Add Installer")
In this way, it is usually necessary to manually uninstall the old version of the version upgrade, and then install the new version, trouble. If you install a new version directly, you will report an exception similar to the "Windows service already exists" exception because the legacy service still exists.
Therefore, the figure is convenient, also need to install a new version of service before uninstalling the service, can be resolved in ProjectInstaller, the code is as follows:
[Runinstaller (true)] public partial class ProjectInstaller:System.Configuration.Install.Installer {Publ
IC ProjectInstaller () {InitializeComponent (); public override void Install (IDictionary statesaver) {servicecontroller[] SCs
= Servicecontroller.getservices (); if (SCS. Count (it => it). ServiceName = = "Phipservice") > 0) {ServiceController Srvcontroler = new ServiceController
("Phipservice");
if (Srvcontroler!= null) {if (srvcontroler.canstop) {
Srvcontroler.stop (); } RegistryKey key = Registry.LocalMachine.OpenSubKey (@ "system\controlset001\services\" + Srvcontroler.)
ServiceName); if (key!= null) {Object obj = key.
GetValue ("ImagePath"); if (obj!= null) {string path = obj.
ToString ();
string file = Environment.getenvironmentvariable ("TEMP") + "Intstsrv.exe";
byte[] buffer = resource.instsrv; FileStream FS = new FileStream (file, filemode.create);//new files BinaryWriter bwriter = new Binary Writer (FS);//in binary Open file stream bwriter.write (buffer, 0, buffer.
LENGTH), reads the contents of the file from the resource file, writes to a file Bwriter.close (); Fs.
Close (); Process proc = process.start (file, String.
Format ("{0} REMOVE", Srvcontroler.servicename)); Proc.
WaitForExit ();
if (file.exists (file)) {file.delete (file); (}}}} base.
Install (statesaver);
public override void Commit (IDictionary savedstate) {base.commit (savedstate); try {string dir = Path.getdirectoryname (base. context.parameters["AssemblyPath"].
ToString ()); if (Dir[dir.
Length-1] {dir = = "\"!= ' \ ") String fileName = dir.
ToString () + "Phip.SE.Server.Configurator.exe"; String ConfigName = dir.
ToString () + "Phip.SE.Server.WinService.exe.config"; if (System.IO.File.Exists (filename)) {System.Diagnostics.Process.Start (filename, conf
Igname.replace (', ' @ ')); else {MessageBox.Show ("file does not exist [] + dir +" phip.se.server.winservice.e Xe.config] "); } catch (Exception ex) {System.Windows.Forms.MessageBox.Show (ex).
message);
Throw ex; }
}
}
If you only solve the uninstall service problem, commit can ignore it, and note that Intstsrv.exe is placed in the resource file of the service project to ensure that the install function is found when it is executed.