You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
we are using Vite to run our react frontends. adding those with AddNpmApp works well, but the problem is, that the environment variables are not available to the frontend.
as per Vite Documentation environment variables are only available in vite, if the name begins with VITE_.
so I suggest to add a WithEnvironmentPrefix(string prefix) method to set the prefix for all the exposed environment variables like service URLs, Otlp endpoints etc.
Reacted by Sławomir Rosiek, Simon Wimmesberger and Alexandre Nédélec
Activity
ghost added
needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
sure, you could do that, but this means that you have to adjust the env file whenever a new nev is added, or the name of the service changed. there are multiple workarounds, but I think this should all be configured in aspire, I don't want to maintain multiple configurations with different concerns and have them duplicated.
It would be possible to do this today by writing something like this:
publicstaticclassEnvExtensions{publicstaticIResourceBuilder<T>WithEnvironmentPrefix<T>(thisIResourceBuilder<T>resourceBuilder,stringprefix)whereT:IResourceWithEnvironment{returnresourceBuilder.WithEnvironment(context =>{varkvps=context.EnvironmentVariables.ToArray();// Adds a prefix to all environment variable namesforeach(varpinkvps){context.EnvironmentVariables[$"{prefix}{p.Key}"]=p.Value;}});}}
It would be really great to have option to define extra prefix. It's quite common pattern to increase security. It appears in create-react-app, Vite but also .NET has option to load only prefixed variables: .AddEnvironmentVariables(prefix: "CustomPrefix_")
Current workaround is fine if you have only one reference for API, but it's worst when you have more complicated use case. In my case I have to also reference keycloak in my React application to support authentication.
Just piggybacking here on the above with one extra helpful addition: David's code above will duplicate all the pre-existing keys, this will wipe the old keys as well and it will automatically prefix them with the resource's name for you, to make it a bit simpler
public static class ResourceBuilderExtensions
{
public static IResourceBuilder<ProjectResource> AutoPrefixEnvironmentVariables(
this IResourceBuilder<ProjectResource> builder
)
{
return builder.WithEnvironment(ctx =>
{
var kvps = ctx.EnvironmentVariables.ToArray();
var name = ctx.Resource.Name;
foreach (var p in kvps)
{
ctx.EnvironmentVariables[$"{name}_{p.Key}"] = p.Value;
_ = ctx.EnvironmentVariables.Remove(p.Key);
}
});
}
}
It'll turn the injected env variable services__myAspnetServer__http__0 into agent_services__myAspnetServer__http__0
Which then means you can do this very clean and easy inside MyAgentProject, as an example:
var config = new ConfigurationBuilder()
.AddEnvironmentVariables("agent_")
.Build()
.Get<AppConfig>()!;
Console.WriteLine(config.Services.Server.HTTP[0]);
Console.WriteLine(config.Services.Server.HTTPS[0]);
Coming across this as I'm implementing Aspire at my job. One of the neat things this lets us do, is add a new EnvironmentVariablesConfigurationSource to the end of our stack, but only for the Aspire: prefix. In this way, we can leave everything as it is, and if Aspire happens to be present in development workflows, it can selectively override anything. It wouldn't be nearly as trivial as migrating our existing config stack to accept all env vars last.
I know it's a bit of a cat-and-mouse game, and perhaps there's a better way of doing it, but it makes migration and demoing a bit easier.
Activity
davidfowl commentedon Jan 27, 2024
This feels a little special. I don't think we would want to do this for all environment variables. It would need to be controlled somehow.
dersia commentedon Jan 27, 2024
another possibility would be to add multiple methods like so:
and others if needed.
I don't like the
For*in the name and hope for better suggestionsdavidfowl commentedon Jan 27, 2024
Do you have a running sample today that we can use to validate the suggestion?
cc David Pine (@IEvangelist) as he has built samples using various front end frameworks.
dersia commentedon Jan 30, 2024
Sorry it took me a few days.
I have created a sample project here:
https://github.com/dersia/AspireWithViteSample
dersia commentedon Feb 8, 2024
I would be happy to contribute this change, if chosen to be done.
IEvangelist commentedon Feb 17, 2024
The Vue example frontend is using Vite, see how here:
https://github.com/dotnet/aspire-samples/blob/main/samples/AspireWithJavaScript/AspireJavaScript.Vue/src/components/TheWelcome.vue#L21
It's just mapping the
VITE_WEATHER_APIfrom the env from Aspire. Article here.dersia commentedon Feb 17, 2024
davidfowl commentedon Feb 17, 2024
It would be possible to do this today by writing something like this:
rosieks commentedon Jul 31, 2024
It would be really great to have option to define extra prefix. It's quite common pattern to increase security. It appears in create-react-app, Vite but also .NET has option to load only prefixed variables:
.AddEnvironmentVariables(prefix: "CustomPrefix_")Current workaround is fine if you have only one reference for API, but it's worst when you have more complicated use case. In my case I have to also reference keycloak in my React application to support authentication.
SteffenBlake commentedon May 10, 2025
Just piggybacking here on the above with one extra helpful addition: David's code above will duplicate all the pre-existing keys, this will wipe the old keys as well and it will automatically prefix them with the resource's name for you, to make it a bit simpler
So if you do:
It'll turn the injected env variable
services__myAspnetServer__http__0intoagent_services__myAspnetServer__http__0Which then means you can do this very clean and easy inside MyAgentProject, as an example:
maddymontaquila commentedon Jun 19, 2025
We've discussed this in general for JS in #8350
svengeance commentedon Oct 1, 2025
Coming across this as I'm implementing Aspire at my job. One of the neat things this lets us do, is add a new EnvironmentVariablesConfigurationSource to the end of our stack, but only for the
Aspire:prefix. In this way, we can leave everything as it is, and if Aspire happens to be present in development workflows, it can selectively override anything. It wouldn't be nearly as trivial as migrating our existing config stack to accept all env vars last.I know it's a bit of a cat-and-mouse game, and perhaps there's a better way of doing it, but it makes migration and demoing a bit easier.