views:

1210

answers:

5

Currently I have to uninstall the old version of my service before I install the new version. I am pretty sure this has something to do with it not being smart enough to update or remove the old service entries before adding the new ones.

Is there a way to have the installer skip registering the service if it already exists? (I can assume the installation folder and service name do not change between versions.)

Also, is there a way to automatically stop the service when uninstalling?


Edit:

I am using MSI packages and the Visual Studio setup project.

A: 

Can't you just stop the service, and overwrite the service executable, and then restart the service?

jalf
Sure, if I were doing everything by hand. But this needs to be wrapped in a MSI package.
Jonathan Allen
A: 

You can make separate DLL which service would load and call every time it does its work. Make sure that service unloads DLL after use.

Use should load it into separate Application Domain.

SO http://msdn.microsoft.com/en-us/library/c5b8a8f9.aspx

Din
This is something he can do BUT once the DLL is loaded by the service, the service won't be able to unload it. +1 for the idea.
Daok
+6  A: 

I do not use Visual Studio setup projects, so I might be wrong, but it seems it has no support for the ServiceInstall and ServiceControl tables which are standard features of Windows Installer. Those two tables are specially for installing and updating services....

Wix does support it (see this example), Maybe you can create a merge module, and use that in your project.

Otherwise this might help: Installing Services with Visual Studio (Phil Wilson)

Wimmel
Thanks, that's exactly the background I need to fix this.
Jonathan Allen
+3  A: 

Use sc tool from a command line to stop and start the service:

sc stop {name of your service}
sc start {name of your service}

When the service is stopped, update the corresponding files and then start the service again. You should be able to do that from your installer as well. If you use Wix for your installer, then take a look at ServiceControl element.

David Pokluda
This works fine on Windows XP and later, but sc.exe is not included with Windows 2000; you might need to include your own copy if you plan to support that operating system.
CodeSavvyGeek
+4  A: 

I've done this with WiX, which generates .MSI files using the ServiceInstall & SeviceControl commands:

<Component Id='c_WSService' Guid='62ed8518-b644-4943-be63-9eaf665941d2'>
    <File Id='f_WSService' Name='WSService.exe' Vital='yes' Source='..\wssvr\release\wsservice.exe' />
    <ServiceInstall Id='WSService.exe' Name='WSService' DisplayName='[product name]' Type='ownProcess'
        Interactive='no' Start='auto' Vital='yes' ErrorControl='normal'
        Description='Provides local and remote access to [product name] search facilities.' />
    <ServiceControl Id='WSService.exe' Name='WSService' Start='install' Stop='both' Remove='uninstall' Wait='yes' />
</Component>

This stops the service, installs the new version and re-starts the service.

Ferruccio
Nice. I'm not looking forward to learning WiX, but it seems like that's the right choice moving forward.
Jonathan Allen