Problem in installing windows service

Jul 1, 2016 at 8:25 AM
using System;
using System.IO;
using Microsoft.Deployment.WindowsInstaller;
using Utils;
using WixSharp;
using WixSharp.CommonTasks;

namespace ServiceSetup {
    class Program {

        public static void Main() {
            try {
                var project =
                    new Project("ServerDB",
                        new Dir(@"%ProgramFiles%\Napa Ltd.\ServerDB2"),
                        new ElevatedManagedAction(CustomActions.InstallService, Return.check, When.After,
                            Step.InstallFiles, Condition.NOT_Installed),
                        new ElevatedManagedAction(CustomActions.UnInstallService, Return.check, When.Before,
                            Step.RemoveFiles, Condition.BeingRemoved));

                project.GUID = new Guid("2820A8D7-60E0-45B9-8E4C-D66F29393E56");
                project.OutFileName = "ServiceSetup";
            } catch (System.Exception ex) {

    public class CustomActions {
        public static ActionResult InstallService(Session session) {
            return session.HandleErrors(() =>{
                var result = Utility.GetServerDbInstalledPath();
                    throw new Exception(result.Error);
                var result2 = Utility.GetServerDbPrPath();
                if (result2.IsFailure)
                    throw new Exception(result2.Error);
                string cmd = "";
                cmd = string.Format("--dbpath \"{0}\" --serviceName \"{1}\" --serviceDisplayName \"{2}\" ",
                        Path.Combine(result2.Value,Constants.FOLDER_PR), Constants.MONGO_DB_SERVICE_NAME, Constants.MONGO_DB_SERVICE_DISPLAY_NAME);
                cmd += string.Format("--install --storageEngine wiredTiger --directoryperdb --logpath \"{0}\\system.log\"", Path.Combine(result2.Value, Constants.FOLDER_PR));
                Tasks.InstallService(Path.Combine(result.Value,"bin",Constants.MONGO_DB_EXE), true,cmd);
                Tasks.StartService(Constants.MONGO_DB_SERVICE_NAME, false);

        public static ActionResult UnInstallService(Session session) {
            return session.HandleErrors(() => {
                //Tasks.StopService("WixSharp.SimpleService", false); //no need to call as system stop the service on uninstall anyway
                Tasks.InstallService(session.Property("INSTALLDIR") + "MyApp.exe", false);
Above is my code.

In the log I get this error
System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Program Files\Napa Ltd\ServerDB\\mongod.exe' or one of its dependencies. The system cannot find the file specified..
   at WixSharp.CommonTasks.Tasks.InstallService(String serviceFile, Boolean isInstalling, String args)
   at ServiceSetup.CustomActions.<>c__DisplayClass0_0.<InstallService>b__0() in C:\MyDrive\work\ServerDB\DatabaseInstaller\ServiceSetup\Program.cs:line 47
   at WixSharp.Extensions.HandleErrors(Session session, Action action)
Does it mean I have to embed the .exe file? I get the location of the .exe file from the resgistry so I do not know it when building it.

What should I do in this case?
Jul 1, 2016 at 9:28 AM
I check Wix Sharp code. It seems its somehow coming from InstalUtil.exe . Any idea why?
Jul 1, 2016 at 11:43 AM
Edited Jul 1, 2016 at 1:39 PM
You are trying to install mongod.exe as a service with InstallUtil.exe (via Tasks.InstallService). Sorry but it's guaranteed to fail.
  1. As far as I know mongod.exe is not a service so it cannot be installed as one.
  2. mongod.exe is not an assembly so even if it was a service it could not be installed with InstallUtil.exe. It cannot even load it as it's is a native app.
Jul 1, 2016 at 11:54 AM
Thanks for pointing that out to me.

I changed my code to do this :
string cmd = "";
                cmd = string.Format("--dbpath \"{0}\" --serviceName \"{1}\" --serviceDisplayName \"{2}\" ",
                        Path.Combine(result2.Value, Constants.FOLDER_PR), Constants.MONGO_DB_SERVICE_NAME, Constants.MONGO_DB_SERVICE_DISPLAY_NAME);
                cmd += string.Format("--install --storageEngine wiredTiger --directoryperdb --logpath \"{0}\\system.log\"", Path.Combine(result2.Value, Constants.FOLDER_PR));
                var r = new ExternalTool {
                    ExePath = Path.Combine(result.Value, "bin", Constants.MONGO_DB_EXE),
                    Arguments = cmd
                Tasks.StartService(Constants.MONGO_DB_SERVICE_NAME, false);
and now it works.

Thanks for the help