How to fix: In Azure Devops connection string is null despite setting Variable in the build Pipeline

≡放荡痞女 提交于 2021-01-28 07:38:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!