Error while deploying to Azure App Service with Azure Key Vault dependency

Sarah 161 Reputation points
2021-10-22T06:22:22.627+00:00

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.

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,192 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
850 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sarah 161 Reputation points
    2021-11-14T15:19:05.31+00:00

    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.

    1 person found this answer helpful.