是否只有在我的 launch.json 文件中初始化 dotnet api 后才能启动我的 angular SPA?

Is it possible to start my angular SPA only after the dotnet api gets initialized in my launch.json file?

在 Visual Studio 代码中调试时,只有在我的 dotnet API 在我的复合配置语句中初始化后才能启动我的 Angular SPA 吗?

我下面的复合配置同时启动(SPA 和 Dotnet),但在某些情况下,不是所有情况下,SPA 在 dotnet API 初始化之前启动并完成初始化,SPA 立即调用主页上 API 中的数据库。因此,在 SPA 完成初始化但 API 尚未完成初始化的情况下,我会看到如下所示的错误。 所以我必须刷新浏览器一次或两次,直到 dotnet API 首先完成初始化。

调试时两个项目的初始化,哪个先完成,好像完全是随机的?

Failed to load resource: net::ERR_CONNECTION_REFUSED [http://localhost:5001/api/matter/options]

这是我的复合配置:

"compounds": [{
  "name": "Server/Client",
  "configurations": ["Dotnet", "Chrome"]
}],
"configurations": [{
  "name": "Chrome",
  "type": "chrome",
  "request": "launch",
  "url": "http://localhost:4200",
  "webRoot": "${workspaceFolder}/Auto2-SPA"
}, {
  "name": "Dotnet",
  "type": "coreclr",
  "request": "launch",
  "preLaunchTask": "build",
  // If you have changed target frameworks, make sure to update the program path.
  "program": "${workspaceFolder}/Auto2.API/bin/Debug/netcoreapp2.2/Auto2.API.dll",
  "args": [],
  "cwd": "${workspaceFolder}/Auto2.API",
  "stopAtEntry": false,
  // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser

  "env": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "sourceFileMap": {
    "/Views": "${workspaceFolder}/Views"
  }
}]

有一个名为 wait-on 的 npm 包。此插件等待特定操作完成,然后将控制权交还给下一行执行。

你可以做什么,假设你有一些开放端点(没有安全的端点),你可以在启动你的 angular 应用程序之前配置它,这样每当后端准备就绪时,然后只有前端 - end end 会被编译执行。

您可以配置为package.json

  1. npm install -g wait-on
  2. 修改 start 脚本如下(将 http://localhost:8000/foo 替换为您的端点)
{
  ...
  "scripts": {
    "ng": "ng",
    "start": "wait-on http://localhost:8000/foo && ng serve",
  ...

在此之后,当您使用 npm start 启动 angular 应用程序时,它将等待端点并仅在端点可用时启动。

您可以使用APP_INITIALIZER,一个在应用程序启动时立即调用的函数,直到完成后才初始化应用程序。

例如,您可以 ping APIs 看它们是否准备就绪,如果没有,重复 ping N 次直到超时。

我做了一个 stackblitz 来或多或少地向你解释它是如何工作的,即使它有很多多余的东西。由于我必须模拟 ping API,因此我不得不在调用中插入不同的超时,以使它们至少看起来逼真。

https://stackblitz.com/edit/angular-rnuwn9

我希望我明白你需要什么