Aspire launches the client of a hosted Blazor WebAssembly app on a different random port every time the AppHost is launched
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
**EDIT: After researching the problem more extensively, I changed the title of this issue from "Question - how to know the URL assigned to a hosted Blazor WebAssembly app" to "Aspire launches the client of a hosted Blazor WebAssembly app on a different random port every time the AppHost is started".
The below is how my question was originally worded, but the root cause and the steps to reproduce are in my comment of 15th January.**
Some time ago, Bossman Dan Roth [reported here](https://github.com/dotnet/aspnetcore/issues/18612), that the URL (e.g. `localhost:xxxxx`) of a hosted Blazor WebAssembly app "should be generated randomly by Visual Studio". That issue has been resolved and that's certainly what's happening on my machine. Perfect.
My question is, how can I determine what URL will be (or has been) assigned by Visual Studio, please?
I either need to control the URL (set it myself in launchSettings or my Aspire AppHost) or I need to read it (from either the Blazor server project or my Aspire AppHost) once it has been assigned by Visual Studio. I don't mind which.
**The use case**
My hosted Blazor WebAssembly app is a client of a Web API. The client's URL should get listed in the Web API's CORS configuration. For example (in the web API)
```
// Get a list of clients allowed to access the Web API.
var clients = builder.Configuration.GetSection("Clients").Get() ?? [];
// Getting the URL of the Blazor server app was easy using existing Aspire features. Hopefully I can figure out how to get the Blazor WebAssembly app onto this list too.
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(clients); // Allow the list of clients to access the Web API
policy.AllowAnyMethod();
policy.WithHeaders("X-Requested-With");
policy.AllowCredentials();
});
});
```
I thought perhaps it could be set in the launchSettings.json file of the client app, like this:
```
{
"profiles": {
"Blazor.Client": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:7279;http://localhost:5279",
"launchUrl": "https://localhost:7279/Counter",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
```
But the content of that file seems to be ignored by Visual Studio when launching a Blazor WebAssembly app. Can anybody point me in the correct direction please? How do I know the URL at which my Blazor WASM app is going to run early enough in the app lifecycle that I can add it as a client of the web API?
Contributor guide
Assessment
This issue has not been assessed yet.