问题
I have problem with build pipeline in Azure Devops, with variables from build pipeline not replacing empty configuration in appsettings.json. Below there is more details.
My current test project is build using asp.net core technology and is connected to SQL server. I also use Entity Framework Core and autofac.
To connect to SQL server I use appsettings.json configuration:
{
"ConnectionStrings": {
"AzureDbConnectionString": ""
}
}
but my credentials are stored in secrets.json
{
"ConnectionStrings": {
"AzureDbConnectionString": "Server=tcp:servername-db-srv.database.windows.net,1433;Initial Catalog=dbname-db;Persist Security Info=False;User ID=user;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
}
}
I've configured my build pipeline variable: Name:
ConnectionStrings--AzureDbConnectionString
Value:
Server=tcp:servername-db-srv.database.windows.net,1433;Initial Catalog=dbname-db;Persist Security Info=False;User ID=user;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
The problem occurs while running Generate Migration Scripts in the build pipeline.
Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Microsoft.EntityFrameworkCore.DbContextOptions[] -> λ:Microsoft.EntityFrameworkCore.DbContextOptions -> λ:Microsoft.EntityFrameworkCore.DbContextOptions`1[[AspNetAutofacAzure02.Data.SchoolContext, AspNetAutofacAzure02, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. ---> System.ArgumentException: The string argument 'connectionString' cannot be empty.
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
Like I mentioned, it looks like the variable isn't used while generating the script.
Is there anything wrong I do here?
回答1:
Did you try ConnectionStrings:AzureDbConnectionString
? That's the normal format for overriding appsettings.json. Or ConnectionStrings__AzureDbConnectionString
.
If the value is coming from keyvault in the format ConnectionStrings--AzureDbConnectionString
, then just map a new variable: ConnectionStrings:AzureDbConnectionString
= $(ConnectionStrings--AzureDbConnectionString)
回答2:
You can add Set Json Property task to replace the ConnectionStrings. You may need to install this task to your organization first.
Usage:
First click the 3dots on the right side to locate your appsettings.json file.
Then set the Property path value to ConnectionStrings.AzureDbConnectionString
.
Last set the Property value to your pipeline variable $(ConnectionStrings--AzureDbConnectionString)
来源:https://stackoverflow.com/questions/58113690/how-to-fix-in-azure-devops-connection-string-is-null-despite-setting-variable-i