0
votes

I have a MSI setup to Install and Uninstall my Windows Service App.. I have written the code to remove Windows Service in Uninstall function.. here, my problem is, the MSI setup trying to delete files before the Uninstall function call and then I am receiving the below warning msg:

enter image description here

My Installer class code:

[RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
            //Add custom code here
        }


        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
            //Add custom code here
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            Program.InstallService();
        }

        public override void Uninstall(IDictionary savedState)
        {
            Program.UnInstallService();
            base.Uninstall(savedState);
        }
    }

Can anyone help me to bypass this error msg?

3

3 Answers

1
votes

You don't have to handle the installation/uninstallation of the windows service using installer class. MSI is capable of handling the installation/uninstallation.

1) You have to add the service installer class to your windows service project. to do that, Open your service class in design mode and right click on the design view and Find add installer option. Once you add the class you need to go to the properties of service installer class and make necessary changes(service name etc).

enter image description here

[2] Go to FileSystemEditor view of your windows service installer project(MSI), there you should see any dlls or any other files which will be deployed during the installation. Right click there and select Add > Project Output. In the pop up window select "Primary Output" and select your windows service project.

enter image description here

[3] Check on the installer project and select Custom Actions Editor option available on top of the solution explorer window. Now you would see Install/Uninstall/Rollback/Commit actions in the left side. right click on every action and pop up window will show up, Select Application Folder and select the Primary output you have added in step 2. Rebuild the MSI project now when you install/uninstall the MSI windows service will also be installed/uninstalled automatically.

enter image description here

0
votes

One of the many problems with Visual Studio Deployment Projects is that it fails to expose the underlying Windows Installer ServiceInstall and Service control tables. If you were using these you'd likely not have any problems as the native functionality works very well. Custom actions like ServiceInstaller typically reinvent the wheel with a more fragile solution.

The best thing to do here is to factor your service out into a merge module authored using Windows Installer XML (WiX). Then consume this merge module in your MSI project. This way you don't need any custom actions and it just works.

Or, just be rid of this project type completely and rewrite your installer using WiX. I maintain an opensource project called Industrial Strength Windows Installer XML (IsWiX) and it only take a few minutes to write an installer like this. You can see a demo at:

Building and Deploying a Windows Service using IsWiX

0
votes

That dialog is not telling you the MSI is trying to delete files before the uninstall - it's just a files in use dialog telling you that the service and maybe any Dlls it's loaded are in use and will not be removed. It's prompting you to stop the service BEFORE it continues and fails to remove the running service

In the limited environment of Visual Studio setup projects (which is apparently what you are using) you seem to be doing this incorrectly. Base.Uninstall () will uninstall the service - you don't need to do it, and that Base.Uninstall will run before the files are deleted. It's even possible that your unnecessary Program.UninstallService is causing an issue, but of course we can't see the code. All most people add to Base.Uninstall is code to shut down the service and wait for it to completely finish. It will also be an issue if you do not properly close handles. If there is anything in your code that does not (for example) do ServiceConteroller.Close() then you will see this message because your code has a reference to the service.