【问题标题】:My .msi file installer during installation ask to close currently run console application or windows service我的 .msi 文件安装程序在安装过程中要求关闭当前运行的控制台应用程序或 Windows 服务
【发布时间】:2018-07-25 10:35:58
【问题描述】:

我正在尝试通过我的控制台应用程序或 Windows 服务来运行批处理。在批处理中,我编写了运行我的 Setup.msi 并创建 InstallLogFile 的命令。

  1. 尝试使用控制台应用程序。运行 Setup.msi 并弹出“在继续安装之前应关闭‘MyConsoleApplication’”时出现问题。而且我可以选择检查自动关闭应用程序并在设置完成后尝试重新启动它们。

  2. 尝试启动我创建的 Windows 服务。这是运行 Setup.msi 后有点复杂的原因,我没有看到任何弹出消息,只是通过日志我发现我的服务已停止(可能我的 Setup.msi 需要像 1. case 但是我不知道为什么)安装后,再次启动服务并再次执行整个逻辑,这是我想避免的。

    • Setup.msi 是通过 Wix 工具集创建的
    • Setup.msi 在这两种情况下都会通过 Web 客户端从 Dropbox 自动下载(用 C# 编写代码)

首先在 Windows 服务的 OnStart 方法中,我在处理整个进程的单独线程方法中调用了 Update()。

protected override void OnStart(string[] args)
    {
        try
        {
            Task.Run(() => 
            {
                try
                {                        
                    Update();                      
                }
                catch (Exception ex)
                {

                }
                finally
                {

                }
            });
        }
        catch (Exception ex)
        {

        }
    }

我不包括整个代码,只是没有日志信息的逻辑。

在更新方法中,一些核心逻辑是: 1.下载Setup.msi 2.关闭进程并停止部分服务 3. 使用启动 Setup.msi 安装的代码启动运行 .bat 的进程

public void Update()
    {
        var downloadLink = @"someLinkOnDropbox";

        // try to download
        try
        {
            using (var client = new WebClient())
            {                   
                client.DownloadFile(downloadLink, myDirLocation);
            }
        }
        catch (Exception ex)
        {            

            return;
        }            

        while (ProcessIsRunning("someProcess") || ServiceIsRunning("someService1") || ServiceIsRunning("someService2") || ServiceIsRunning("someService3"))
        {
            // try to terminate process
            StopProcess("someProcess");

            // try to stop the service
            StopService("someService1");
            StopService("someService2");
            StopService("someService3");                
        }

        // wait 5 sec process and services are stopped
        Thread.Sleep(5000);

        // path to the my .bat file on D: disc which run Setup.msi (also downloaded on my D: disc)
        const string path = @"somePath";
        const string fileName = @"batName.bat";
        const string workingDirectory = @"myDir";

        try
        {          
            Task.Run(() => 
            {
            StartProcess(path, fileName, workingDirectory);
            });
        }
        catch (Exception ex)
        {

        }
    }    

此外,StopProcess、StopService、ProcessIsRunning 和 ServiceIsRunning 方法是定制的,在这一点上工作正常,我没有问题。

方法StartProcess如下:

public void StartProcess(string path, string fileName, string workingDirectory)
    {
        ProcessStartInfo psi = new ProcessStartInfo(path)
        {
            FileName = fileName,
            WorkingDirectory = workingDirectory,
            UseShellExecute = true,
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Hidden
        };

        //  Try to start the process.
        try
        {
            Process p = new Process {StartInfo = psi};
            p.Start();
            Log.Instance.Warn($"Process started");
            p.WaitForExit();
            Log.Instance.Warn($"Process finished");
        }
        catch (Exception ex)
        {

        }
    }

.bat 文件中的代码: msiexec /i Setup.msi /l*v SetupInstall.log INSTALL_SETTINGS_FOR="deviceID"

【问题讨论】:

  • 不清楚您实际要问的是什么。您是否正在尝试让您的应用程序自行更新?
  • 不尝试更新自己,关键是我创建了 Windows 服务。 - 启动此 Windows 服务时,从保管箱下载 Setup.msi - 然后从同一个 Windows 服务运行运行 .batch 文件的新进程。 - 这个 .batch 文件运行 Setup.msi,它将安装一些应用程序。但是在这个安装过程中我的windows服务被停止了,因为Setup.msi需要,为什么我不知道?在它停止并安装完成后,windows服务再次启动,这是我想避免的情况。
  • 请给我们一些代码。您究竟是如何启动 .bat 的?您究竟是如何触发 .msi 安装的?
  • @rs232 请在已编辑的问题中找到一些代码。

标签: c# .net windows service


【解决方案1】:

这就是让我对我的服务产生问题的原因。

我想通过 MyService 更新的应用程序位于同一位置并使用了一些相同的文件。所以在更新过程中,当MyService通过批处理运行它时,安装需要停止服务导致服务使用了一些需要更新的文件,导致应用程序也使用它。

解决方案是将服务安装位置移动到新目录中。就像是 D:\MyApp 用于应用程序,以及 D:\MyApp\MyService 为服务, 在这种情况下,MyApp 和 MyService 之间的共享文件可以在 MyApp 中更新,而无需 MyApp 停止 MyService。

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多