[英]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.