Read environment variables from launchSettings.json during Run/Debug
Hi all,
I am have a .Net Angular SPA where I am adding more application and libs so I can share frontend code. The objective is to run a different container for each app, so I am trying to pass the configuration of the app to run during build time.
I added these configuration in `launchSettings.json`:
"AppName": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://0.0.0.0:5026",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Local",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy",
"NG_APP_SPA_NAME": "AppName",
"NG_APP_SPA_PORT": "44416",
"NG_APP_SPA_SUBFOLDER": "app-name"
}
}
And changed the csproj like this:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\projects\$(NG_APP_SPA_SUBFOLDER)\</SpaRoot>
<SpaProxyServerUrl>http://localhost:$(NG_APP_SPA_PORT)</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
"npm start" should run this script "ng serve $NG_APP_SPA_NAME --host 0.0.0.0 --disable-host-check --port $NG_APP_SPA_PORT -c development" but when I run/debug with Rider it seems like the environment variables aren't loaded correctly because the generate spa.proxy.json file doesn't have the expected values:
{
"SpaProxyServer": {
"ServerUrl": "http://localhost:", --> missing port
"LaunchCommand": "npm start",
"WorkingDirectory": "/Users/user/AppName/ClientApp/projects/", --> missing folder name
"MaxTimeoutInSeconds": "120"
}
}
I also noticed that ASPNETCORE_ENVIRONMENT doesn't have a value.
How can I fix this issue?
Please sign in to leave a comment.
I am starting to suspect that those environment variables aren't available at build time...
Hi Yiyi Chen!
It seems like Run/Debug configuration that you use doesn't know about your environment variables. Right-click on the launchsettings.json and choose "Generate configurations" in the context menu. Here you can find more about this.