[英]Ask and store configuration variables during installation of a windows service
[英]My .msi file installer during installation ask to close currently run console application or windows service
我正在尝试通过控制台应用程序或Windows服务运行批处理。 批处理中,我编写了运行Setup.msi并创建InstallLogFile的命令。
尝试使用控制台应用程序。 运行Setup.msi并出现“在继续安装之前应先关闭'MyConsoleApplication'”消息时,会出现问题。 我可以选择检查自动关闭应用程序,并在安装完成后尝试重新启动它们。
尝试启动我创建的Windows服务。 这是运行Setup.msi后有点复杂的原因,我没有看到任何弹出消息,仅通过日志我发现我的服务已停止(可能是我的Setup.msi需要像1.情况,我不知道为什么),然后在安装后再次启动服务,并再次执行整个逻辑,这是我想避免的。
首先在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.启动运行.bat的进程,并使用启动Setup.msi的代码进行安装
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”
这就是使我的服务出现问题的原因。
我要通过MyService更新的应用程序位于同一位置,并使用了一些相同的文件。 因此,在更新过程中,当MyService通过批处理运行它时,安装需要停止服务,因为服务使用了一些需要更新的文件,导致应用程序也使用了它。
解决方案是将“服务”安装位置移到新目录中。 诸如D:\\ MyApp(针对应用程序)和D:\\ MyApp \\ MyService(针对服务)之类的东西,在这种情况下,可以在MyApp中更新MyApp和MyService之间的共享文件,而无需MyApp停止MyService。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.