Powershell 等待 dotnet run 在某个端口上启动应用程序 [英] Powershell wait for dotnet run to launch application on some port
问题描述
我正在编写一个脚本,它应该一个接一个地运行两个 dotnet 应用程序.一个在端口 5000 上,第二个在端口 5001 上,根据他们的 launchSettings.json
I'm writing a script that is supposed to run two dotnet application one after the other. One on port 5000 the second one on port 5001 according to their launchSettings.json
到目前为止,这是运行应用程序的脚本:
So far this is the script that runs the applications:
$app1ProjectFolder = '../src/App1'
$app2ProjectFolder = '../src/App2'
Write-Host "STARTING APP1" -foreground Green
Push-Location $app1ProjectFolder
$dotnetRunCommandApp1 = 'run'
$app1Process = Start-Process dotnet -ArgumentList $dotnetRunCommandApp1 -PassThru
Pop-Location
Write-Host "STARTING APP2" -foreground Green
Push-Location $app2ProjectFolder
$dotnetRunCommandApp2 = 'run'
$app2Process = Start-Process dotnet -ArgumentList $dotnetRunCommandApp2 -PassThru
Pop-Location
我需要的是脚本在启动第二个应用程序之前等待第一个应用程序完成启动或可在其指定端口上访问.
What I need is for the script to wait for the first app to finish launching or be accessible on it's designated port before launching the second app.
推荐答案
您可以使用:
while (!(Test-NetConnection localhost -Port 5000).TcpTestSucceeded) { Start-Sleep 1 }
无限期地等待端口准备就绪.
To wait indefinitely for the ports to be ready.
您可以添加最长等待时间,例如说应用保持运行但无法侦听,或者被防火墙设置.
You could add a maximum wait time e.g. say the app stayed running but failed to listen, or was firewalled.
这篇关于Powershell 等待 dotnet run 在某个端口上启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!