Apologies for the delayed response. Thank you for the reply, I was able to solve this problem by updating my connection string to Azure database from localdb.
Error while deploying to Azure App Service with Azure Key Vault dependency
When I deployed the app to Azure, I get HTTP ERROR 500. But local setup works fine as excepted.
Log file message below while accessing the Azure endpoint:
>An error occurred using the connection to database 'TestDB' on server '(localdb)\MSSQLLocalDB'.
An exception occurred while iterating over the results of a query for context type 'ItemApp.Infrastructure.Repository.ItemDBContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904):
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote connections.(provider: SNI_PN11, error: 52 - Unable to locate a Local Database Runtime
installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
I installed SQL Server Express with Localdb option since it said "Unable to locate a Local Database Runtime installation" but still getting same error. I am able to deploy the same app to Azure successfully and hit endpoints if I remove Azure Key vault from the code (both while publishing and from the code)
Below is my code for Azure Key Vault:
Connection String is stored in Azure Key Vault as below:
Server=(localdb)\MSSQLLocalDB; Database=TestDB; Trusted_Connection=True
appsettings.json file:
// AzureAD setting for Authentication
"AzureAd": {
"Instance": "",
"ClientId": "",
"TenantId": "",
"Audience": "",
"Issuer": "",
"ClientSecret": ""
},
//Key Vault setting
"VaultConfig": {
"VUrl": ""
}
Program.cs file:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
// calls Azure Key Vault with values supplied by the appsettings.json file
.ConfigureAppConfiguration((context, config) =>
{
var builtConfig = config.Build();
string vUrl = builtConfig["VaultConfig:VUrl"];
string tenantId = builtConfig["AzureAd:TenantId"];
string clientId = builtConfig["AzureAd:ClientId"];
string clientSecret = builtConfig["AzureAd:ClientSecret"];
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var client = new SecretClient(new Uri(vUrl), credential);
config.AddAzureKeyVault(client, new AzureKeyVaultConfigurationOptions());
});
}
Startup.cs code:
// "DevConnection" is the key we have used to store database Connection String.
services.AddDbContext<ItemDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DevConnection"),
optionsBuilder => optionsBuilder.MigrationsAssembly("ItemApp.Infrastructure")
));
May I know where I am going wrong? Thanks in advance.