install windows service in custom actions - c#

I have created a c# 4.0 windows service. I have created an installer project (.msi) for it which installs the service to a particular folder.
To automate the process fully, I would like to install the service as part of the custom actions I have for my installer.
How can I code my custom actions to install or when uninstalling the msi, uninstall the windows service?

You can use the ServiceInstaller class. A quick solution would be to find installutil tool and execute it against your Service.exe, but you have to capture the output to see whether the installation succeeded or not and you don't have much control over Install, Commit, Rollback, and Uninstall phases.

Simple answer: don't. The proper way is to install it using the MSI database itself, i.e. ServiceInstall and ServiceControl tables. Every single "convenient" IDE for MSI creation and also WiX come with primitives to make use of this builtin facility.
The very reason that this is best practice, just like including the COM registration in your MSI instead of calling DllRegisterServer of the COM (DLL) to register is that your application may be defunct at the time the user attempts to remove it.
The database actions can still be executed even by a newer Windows Installer, say after an upgrade of Windows itself, while your code may refuse to run or may not run for other reasons.

Related

Creating an single installer of a solution with WPF as start up project and depend on Windows Service

Asking similar question because no one answered previous question asked
I have a solution with a class library project, a WPF project (start up project) and a Windows Service project. I would like to create one installer so that when the user installs the application then both WPF (UI) and Windows service gets installed.
Most of the resources I found online only talk about one or another but not both. For example this:.
I checked some question on stackoverflow but either they are not answered by any one or vague answers like this question or this one.
Any suggestion how to achieve this?
I'm not an expert on various installer solutions, but yes, this can be done. I know because my project does it.
We've historically used InstallShield, but we are actively moving to InstallAware for reasons unrelated to this discussion. Frankly, I suspect any installer solution (e.g., InstallShield, InstallAware, Wix, etc.) could be used to do this providing that it has the means to execute a batch script as part of the install process (more on this in a moment). In fact, while we are building our new installer using InstallAware, we are temporarily delivering our WPF-based application and Windows Service using a WinZip self-extracting executable. The WinZip self-extractor puts the WPF application in the C:\Program Files\<application>\<version> folder, puts the Windows Service in the C:\Program Files\<application>\Common folder, and then installs and starts the Windows Service.
The trick to all of this, of course, is getting the Windows Service installed. Initially, we used the InstallUtil.exe utility to do this, but we had to err on the side of caution and deliver it with our installer because we couldn't verify whether or not we could depend on the utility being available on the target system. At some point along the way, I read this answer by #Marc Gravell. This provided the springboard to my answer here, which provides detailed instructions for having your Windows Service install itself from the command line without the need for InstallUtil.exe.
So as a set of instructions...
Update your Windows Service based on the details here.
Create a .bat file with the following commands:
cd <PathToWindowsServiceInstallationFolder>
<YourWindowsService>.exe -install
Build the installer for your WPF Service and Windows Service. Note that this should focus on deploying the files to their correct locations. You'll need to include the .bat file as part of the installer. If your installer solution allows you to copy files to a temporary folder that gets deleted at the end of the installation process, copy the .bat file to that location since you won't need it after the installer is finished.
Finally, execute the .bat file from your installer during installation. This will install the Windows Service.
It's really not that complicated all things considered.
The one thing to be aware of is that your users should plan on running the installer as an administrator. Since installing the Windows Service updates the registry, users without administrative privileges might run into problems when trying to install your product.
HTH

How to make one installer dependent upon other, while installing application in windows?

I am struggling with a issue of making one installer dependent upon other.Suppose i have a setup project Through which, i use to make MSI(or setup) of my c# application for deployment on windows machine. Now suppose i have another project whose MSI(or setup) is generated by another setup project.
Now what i want to make is that when my first setup is run than it will first check for whether second MSI(or setup) is installed or not. If its not installed on target machine then it first set up will run and install second setup, once second set up is properly installed then installer of first continues and completes the its installation.
I can't figure it out how can i check whether second setup has installed or not. i.e how to make second installer dependent upon first installer. I am using visual studio 2013 and the project which i am deploying is c# application
First time i am dealing with making setups for application deployment so may be my way of asking may not be good. Thankyou!
In general that is not possible by means of MSI only since you can't run two MSI installers at the same time. You'll need to use some external solution: a self-made script, InstallShield wrapper etc. The only thing you can do within the MSI is to check that a prerequisite application is installed and if it's not - abort the installation.
Yes, you need to use a packaging tool that either offer out of the box support or you will have to write your own code for this.
You have quite few options here:
-InstallShield
-AdvancedInstaller
-Wix
What you are trying to achieve is known as prerequisite or runtimes required by your application.
As J.Tribbiani mentioned in his answer the solution you need is to use a professional tool like Advanced Installer or the others, to configure your setup as a prerequisite. This is the standard procedure for your requirement.
Here is an example tutorial of how to do it with Advanced Installer:
prerequisite configuration
Or a video of configuring an optional prerequisite, i.e. which the users could choose to skip (let's say if they have an equivalent alternative or want to manually install the prerequisite)
Disclaimer: I work on the team building Advanced Installer

Custom uninstalling clickonce application

Presently when i uninstall a .net clickonce app, it doesn't check whether the App is in the running state. Also it doesn't delete files on PC restart also.
So I am planning to overwrite the uninstall method to handle this situation.
Which is the class and method gets called on uninstall? Inplacehostingmanager class has only install related functions.
You cannot implement the functionality for uninstall actions in your main application.
You need to overwrite the registry entries, written by the setup, to run a custom executable. This will then check if your application is still running and remove the files that you might have stored in a custom application folder. Then you call the real ClickOnce uninstall routine and you're done.
A very good tutorial can be found on www.codeproject.com.

Wix: How far can I go with custom action?

I want to build a Wix installer which will detect a few programs of mine and update them.
I have c# code (using another DLL) to check a few things on the system,
then I want to download a table of the most recent version of all the apps,
decide which app I need to update, and then download & update the selected ones.
So my question is, can Wix preform the following actions:
1) run another dll call using c#?
2) download a file from the web and parse it (let's say - also using c#)?
3) go to a link and download an MSI\EXE?
4) install the MSI\EXE (let's say run it on silent mode)?
5) uninstall old other apps from system?
Windows Installer has a mutex that only allows 1 execute sequence per machine and 1 UI sequence per process. One MSI cannot install another MSI due to this limitation. There are hacks around this but they don't follow good design (don't provide proper elevation support or silent install / uninstall support ).
You should use custom actions with care. A properly designed custom action should behave like the standard actions built into Windows Installer. That is support transactional installation whenever possible and be data driven via custom tables.
A better candidate for the stuff you are describing needing to be done would probably be a bootstrapper / chainer such as WiX's Burn feature.
1) - definitely yes
2)-5) you could do it but you should differ msi from "custom bootstrapper" and "custom after install configuration manager". General rule: use msi package only for resource (usually files; in more complex cases - registry, in most cases sql objects) atomic deployment; move all other functionality outside msi (means use wix ony to build msi; create custom utilities for bootstrapper and configuration tool; see wix samples how to integrate three parts together).
1) Yes but keep in mind that if you have a dependency on .NET 3.5 and you are on a box with .NET 4 your custom action will not be able to run.
Besides this your custom action dll is unpacked from the msi into the %TEMP% folder. If you have any dependency to other dlls not stored in the GAC you will fail to load. If you bring in another e.g. C++ dll you have to embed it as resource in your C# dll and unpack it as well to find it.
2) You can do whatever you have the rights for.
3) Sure
4) Only one MSI installation can run at one time. You have to spawn some child process to wait until the current installation is over.
5) Yes sure. The easiest way is to add an Upgrade table to your msi to simply uninstall any software which has this upgrade code. This is the only allowed action where two msis at the same time can be active. Look at the InstallExecute Sequence table for RemoveExistingProducts action.

install/uninstall of windows services during development

I would like to know if there is an easier way to install or uninstall windows services onto dev or test environments without creating setup packages? I am having issues with uninstalling visual studio packages sometimes (even after stopping the service and closing the service management console before uninstall forcing me to reboot the machine). this issue can be overcome by using tools like InstallShield or WIX to create setup packages, but during development it will be easier if I can install the service (along with all the dependent files) in dev or test environment without creating a setup package.
Use installutil, which is part of the .net framework. Now you still need to copy the bin directory to the desired machine and register any COM objects if you are using any.
If remember right,( no access to my dev machine at the moment). Stop the service - and replace the exes and dlls) restart the service. You can script this. You will need to do an initial install, or use the installutil.exe
I found this much easier that installing and uninstalling, all the time.
I find the SC.EXE command to be easy to use during development. Slap it into a NAnt/BuildBuild/Perl/BAT file and instant light weight automation.
I know this is an old question, but I feel this information is highly relevant to anyone struggling with this issue:
The problem comes from a change in the Installer included in Visual Studio. I think the change occurred between VS2005 and VS2008. Regardless, after the change, to have an update installer work properly without uninstalling the prior product, it was recommended that Services be installed via Custom Actions in the Install, Commit, and Rollback phase, but not the Uninstall phase. I believe this is because the change caused the Uninstall action to occur after the new version was installed, uninstalling your service upon update. A Condition of "Not PREVIOUSVERSIONSINSTALLED" is placed on the 3 Custom Actions and Check For Previous Versions must be TRUE. This results in essentially the same result as copying the files over the old ones (but also retains any other install activity such as registering objects, etc.)
This all works great for updates; a new version will install over an existing version, the service remains registered, all is well. However, if you uninstall, your service is left registered, and a fresh install will attempt to register it again, resulting in the 1001 error. I use SC.EXE to delete the service manually when I uninstall to avoid this. You can have a clean uninstall, but it will break update installs, your choice.

Categories