2

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?

Kamil
  • 279
  • 2
  • 10
  • 1
    Did you try `ConnectionStrings:AzureDbConnectionString`? That's the normal format for overriding appsettings.json. Or `ConnectionStrings__AzureDbConnectionString`. – Daniel Mann Sep 26 '19 at 17:13
  • Not sure what tasks you were using in your pipeline. But you can use `Set Json Property task` to replace connectionstring. Check below my answer. – Levi Lu-MSFT Sep 27 '19 at 03:35
  • @DanielMann thanks for solution. Yes the ConnectionStrings:AzureDbConnectionString was working for me in the Azure Devops. The "--" notation is used when managing the secrets stored in Azure Key Vault. – Kamil Sep 28 '19 at 15:33

2 Answers2

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)

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
-1

You can add Set Json Property task to replace the ConnectionStrings. You may need to install this task to your organization first. enter image description here

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)

enter image description here

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Hi Levi, thanks for the answer. This sounds interesting to use Set Json Property task, but I prefer to use Variable to have consistency in the code. Using secrets.json in the code and variables in the Azure Devops. Thanks for replying :-) – Kamil Sep 28 '19 at 15:41