How to access the value of a parameter #10875
-
After upgrading to 9.4 I get a bunch of the following warnings:
What is the recommended way to work with this in the following examples: var mydb =
mySqlServer
.AddDatabase("mydb")
.WithCreationScript(script.Replace("<MigratorPasswordPlaceHolder>", migratorPasswordParameter.Resource.Value));
var migrator =
builder
.AddProject<Migrator>("Migrator")
.WithEnvironment(context =>
{
context.EnvironmentVariables.Add("ConnectionStrings__Migrator", $"Data Source=127.0.0.1,{mySqlServer.Resource.PrimaryEndpoint.Port};Initial Catalog={mydb.Resource.DatabaseName};User ID=migrator_user;Password={migratorPasswordParameter.Resource.Value};TrustServerCertificate=true");
}); In the example code I get the warning everywhere i try to use In this use-case i have a creation script that creates multiple logins with different permissions on the database. And later on, I try to setup a connection string that uses one of those logins. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Required reading https://github.com/dotnet/aspire/blob/main/docs/specs/appmodel.md#values-and-references For this one, there's no great alternative other than reading the configuration value directly. Though I'm surprised you need the password in the SQL script (why do you need that?). cc @sebastienros (we need a callback version of WithCreationScript); var mydb =
mySqlServer
.AddDatabase("mydb")
.WithCreationScript(script.Replace("<MigratorPasswordPlaceHolder>", migratorPasswordParameter.Resource.Value)); For this one, you can use a reference expression to build up the connection string: var expr = ReferenceExpression.Create($"Data Source=127.0.0.1,{mySqlServer.Resource.PrimaryEndpoint.Property(EndpointProperty.Port)};Initial Catalog={mydb.Resource.DatabaseName};User ID=migrator_user;Password={migratorPasswordParameter};TrustServerCertificate=true");
var migrator =
builder
.AddProject<Migrator>("Migrator")
.WithEnvironment(context =>
{
context.EnvironmentVariables.Add("ConnectionStrings__Migrator", expr);
}); The reason .Value is deprecated is because the value might not be set and can be set at runtime using the new parameter value dialog https://learn.microsoft.com/en-us/dotnet/aspire/whats-new/dotnet-aspire-9.4#-interactive-parameter-prompting-during-run-mode |
Beta Was this translation helpful? Give feedback.
Required reading https://github.com/dotnet/aspire/blob/main/docs/specs/appmodel.md#values-and-references
For this one, there's no great alternative other than reading the configuration value directly. Though I'm surprised you need the password in the SQL script (why do you need that?).
cc @sebastienros (we need a callback version of WithCreationScript);
For this one, you can use a reference expression to build up the connection string: