4

We have many questions on stackoverflow regarding uninstallation of Windows Service.
After trying all of them I still fail to uninstall the windows service on new version Installation.

I use setup and deployment project to install/uninstall my project which has a windows service and some other projects.

During installation of newer version, all other projects are successfully re-installed but windows service project fails to Re-install and says:

Error 1001: The specified service already exists.

I referred this link and tried to add code to my Install custom action to Stop the service. If I understood the answer in this link correctly, I have put the code to stop the service inside projectInstaller.cs file of the Service:

public override void Install(IDictionary stateSaver)
{ 
      ServiceController sc = new ServiceController("SareeManagerNotifications");
      if (sc.Status == ServiceControllerStatus.Running)
          sc.Stop();
     base.Install(stateSaver);
}

Custom action pane looks like: enter image description here

Where the highlighted part is the Service.

I also went through this answer which says to set custom action condition as
NOT PREVIOUSVERSIONSINSTALLED.

enter image description here

This doesn't work for me. Where am I going wrong ?

Thanks in advance :)

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • It looks like you are stopping the service, but not actually destroying it. The error says the service already exists, so you have two choices. Either your code nears to tear down the service and then reinstall it, or if the service exists and is running, just stop it, replace your files and then start the service again. – Stephen Connolly Apr 17 '11 at 21:40
  • @Stephen Connolly: Can you pls guide me to how to tear down the service completely. I tried to search it around the internet, but I think I may not be able to put right key words to find it. Thanks – Marshal Apr 18 '11 at 05:11

2 Answers2

5

Try the following steps while editing the windows service:

  1. Right click your SetupProject from visual studio and select custom actions from view.

  2. You will find 4 custom actions install,commit,rollback and uninstall. Right click on each of these actions one after the other and add a custom action

  3. After doing so you will find a select item in project window. In the window select Application folder from look in dropdown. This will list the primary output from windows service, select this and click ok.

  4. Save and build the setup project.

Hope this will solve your issue....

Cosmin
  • 21,216
  • 5
  • 45
  • 60
Harun
  • 5,109
  • 4
  • 38
  • 60
  • @Harun: I already tried with this. But it works when I have Windows Service alone as a project. But if I have 2 or more projects including this service, this method fails. – Marshal Apr 21 '11 at 10:46
  • @Niraj, Primary output from jkamdar,primary output from TodoApp and Primary output from B'dayReminder. Why there are three primary outputs?I think One setup project can use only one primary output. Ensure which windows service's primary output you are using and remove others from each custom action and try. – Harun Apr 22 '11 at 04:16
  • @Harun: Yes there are three projects and three diff startup points of application. All of them have their custom installers and installations, and hence there are 3 primary outputs – Marshal Apr 22 '11 at 04:49
  • 1
    @Niraj, As per my understanding you are using a single setup project for the three windows service projects. Try creating separate setup projects for each of your windows service projects. – Harun Apr 22 '11 at 05:00
  • @Harun: Yes I do so, where two proj are Windows Forms Apps and third is service. If I create separate projects, they work. But y dont they when they are together ? – Marshal Apr 22 '11 at 06:40
  • @Niraj, your code logic is in those windows form apps right? As per my understanding you've to use a single class library to implement your entire logic, add it's reference to the windows service and then create a setup project for the windows service.Hope you know windows services are used to perform some scheduled tasks behind scene. i still do not understand why you are using a form app(project) instead of class library as windows service will not show any UI. – Harun Apr 23 '11 at 05:04
0

Usually this is done through service control operations. Basically, you need the Stop and Delete flags set for uninstall.

Another approach would be to use ServiceInstaller.Uninstall to remove the service.

Cosmin
  • 21,216
  • 5
  • 45
  • 60