VS2010 packaged Windows Service installation package version upgrade solution

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.