How to start a separat Process inside IIS Application, that doesn't close if IIS AppPool is offline?

Viewed 51

I'm trying to update my main application, which is running under a IIS website, with a self made program (.exe). My AppPool is running as LocalService or with user rights. Without hosting it as a IIS application everything works as expected. However as a IIS application, my self made updater is stopping in the middle of the process without throwing a exception. It is always a few milliseconds after the IIS AppPool is offline.

Updater.exe was started with following code:

public bool ExecuteCommand(string domain, string userName, SecureString password, string extractedFolderPath, string arguments)
{
    Arguments.CheckNonEmptyString(extractedFolderPath, nameof(extractedFolderPath));
    if (Directory.Exists(extractedFolderPath) && File.Exists(Path.Combine(extractedFolderPath, UPDATER_EXE_FILENAME)))
    {
        Logger.Info($"Executing command with {UPDATER_EXE_FILENAME} and arguments {arguments} in working directory {extractedFolderPath}");
        var startInfo = new ProcessStartInfo()
        {
            UseShellExecute = true,
            RedirectStandardOutput = false,
            FileName = Path.Combine(extractedFolderPath, UPDATER_EXE_FILENAME),
            Arguments = arguments,
            WorkingDirectory = extractedFolderPath,
            //UserName = userName,
            //Domain = domain,
            //Password = password
        };

        Process process = Process.Start(startInfo);
        Logger.Info($"Startet update process. [PID: {process.Id}]");
        return process != null;
    }
    else
        return false;
}

I'm seeing the separat process with it`s own id inside the Task Manager. Is my process still connected with the AppPool used for my main application? If not why is it stopping without throwing a exception? (I'm using a try catch statement over my code, but no exception logged)

Code for stopping AppPool:

Logger.Debug($"Process with id '{processId.Value}' will be shut down!");
Process.GetProcessById(processId.Value).Close();
if (ProcessExists(processId.Value))
    Process.GetProcessById(processId.Value).Kill();

Last logs:

Updater-2022-06-28 12:10:16-Debug: Process with id '14868' still running!
   at Updater.IISHelper.StopApplication(ApplicationPool pool, Nullable`1 processId)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Debug: Process with id '14868' will be shut down!
   at Updater.IISHelper.StopApplication(ApplicationPool pool, Nullable`1 processId)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Info: Backup will be created shortly at path: 'F:\path\Temp\software_update\App_V1.0.1\backup'
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Debug: Created directory at path 'F:\path\Temp\software_update\App_V1.0.1\backup\wwwroot\assets\i18n'!
   at Updater.Program.CopyContent(String sourcePath, String targetPath, List`1 ignoreFolders, List`1 ignoreFiles)
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)
   
  ...
  ...
  ...

Updater-2022-06-28 12:10:16-Debug: Created directory at path 'F:\path\Temp\software_update\App_V1.0.1\backup\wwwroot\assets\images'!
   at Updater.Program.CopyContent(String sourcePath, String targetPath, List`1 ignoreFolders, List`1 ignoreFiles)
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Debug: Created directory at path 'F:\path\Temp\software_update\App_V1.0.1\backup\wwwroot\assets\Logs'!
   at Updater.Program.CopyContent(String sourcePath, String targetPath, List`1 ignoreFolders, List`1 ignoreFiles)
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Debug: Created directory at path 'F:\path\Temp\software_update\App_V1.0.1\backup\wwwroot\assets\svg'!
   at Updater.Program.CopyContent(String sourcePath, String targetPath, List`1 ignoreFolders, List`1 ignoreFiles)
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)
Updater-2022-06-28 12:10:16-Debug: Created directory at path 'F:\path\Temp\software_update\App_V1.0.1\backup\brgData\transfer\persist-Brd-2-Skr\persist-Mqtt-Brd-2-Skr'!
   at Updater.Program.CopyContent(String sourcePath, String targetPath, List`1 ignoreFolders, List`1 ignoreFiles)
   at Updater.Program.CreateBackup(String backupPath, String applicationPath)
   at Updater.Program.Main(String[] args)

Update

I could not solve the problem, therefore I changed my approach. I'm now using a windows service listening on new updates and starting them with my self made updater. Update process did not change but work like a charm now. My conclusion is that IIS is making a connection between processes even if they are started as own process and not as a subprocess.

0 Answers
Related