Connected services can be any Azure Services like Azure KeyVault
, Cosmos DB
.
Follow the below steps to Add Connected Service
.
Web API
before adding service dependencies
to the Connected Services
.

Right click on the Project
folder => Add
=> Connected Service
=> click on Add a service dependency
=> select the required Service - Azure Key Vault
/ Azure Cosmos DB
.


- By default, the below code is added in the
Program.cs
.
var keyVaultEndpoint = new Uri(Environment.GetEnvironmentVariable("VaultUri"));
builder.Configuration.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
Added Azure Cosmos DB
- Here you will get an option to store
ConnectionString
in the already added KeyVault
.

After adding service dependencies

The below packages are added in the .csproj
file
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.0.0" />
<PackageReference Include="Azure.Identity" Version="1.6.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.21.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="6.0.1" />
- New file with name
serviceDependencies.json
will be created with dependencies
.
{
"dependencies": {
"secrets1": {
"type": "secrets",
"connectionId": "VaultUri",
"dynamicId": null
},
"cosmosdb1": {
"type": "cosmosdb",
"connectionId": "ConnectionStrings"
}
}
}
By using Connected Services, few configurations like adding NuGet Packages, settings in launchSettings.json
are configured by default.
In launchSettings.json
, below settings are added automatically.

I wouldn't think that you would then have to do the same definitions in the appsettings.json file, because then what would the point of the connected services tab be?
- If you have the
KeyVault/DB
settings in appsettings.json
and on top you have added Connected Services, then the values from appsettings.json
will be replaced with the values from Connected Services
at run time.
References taken from Visual Studio Connected Services and MSDoc