views:

360

answers:

5

What are the necessary items need from creating a deployment project for a windows service? I need to uninstall the previous version of the service before I install the new version.

A: 

It's pretty much just call:

net stop "MyService"
intallutil.exe /u MyService.exe

// Copy your exe into place
installutil.exe MyNewService.exe

// optional
net start "MyService"

This stops the old service, uninstalls it, then copies the new into place, installs it, and (optionally) starts it.

Most installers make this quite easy. The only other "gotcha" is to make sure your installer is restricted to install onto a local drive, not a network path, if the service is going to be installed to run automatically at startup.

Reed Copsey
How do I include that batch file script in the deployment project?
Michael Kniskern
You need to create a CustomAction and have it execute those commands.
Reed Copsey
A: 
  1. In Visual Studio go to your Service file (myservice.vb or myservice.cs) in your service Project.

  2. Right click the design view, and choose properties.

  3. At the bottom of the Porperties window theres an Add Installer link. Click it.

  4. Check out the ServiceInstaller and ServiceProcessInstaller properties.

  5. Compile your project

  6. to uninstall the previous version, in the command prompt, run: Installutil.exe /u c:\myfolder\myservicefile.exe

  7. to install, in the command prompt, run: Installutil.exe c:\myfolder\myservicefile.exe

thats it.

Installutil.exe lives in the framework folder, somewhere in your Windows directory. It doesn't have to be included with your project.

andy
+1  A: 

MSDN has a tutorial on creating a Windows Service and installer.

Dour High Arch
A: 

I use NSIS for my apps, and for installing a service you just use the sc command:

ExecWait 'sc create MyService binpath= "$INSTDIR\MyService.exe"'
ExecWait 'sc start MyService'

And for uninstall:

ExecWait 'sc stop MyService'
ExecWait 'sc delete MyService'

Works great.

fret
A: 

If you already created an installer, there is not much additional work to be done for a Setup project. It will let you install and uninstall directly out of Visual Studio. There are good tutorials on the web.

2 service-related issues that you will run into, and how to address them:

  • You uninstalled a service, but you can't reinstall is, because you get the error message that the service is pending deletion and that a reboot is required. => Close the Windows Service Manager. It will release any references, and you can reinstall your service.

  • You deleted a service executable before you uninstalled it, and now you can't 'uninstall' it to clean up the registry. => I have no solution for that one, other than avoiding this entirely.

cdonner