Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, September 26, 2020

Configure / Bind application with Azure Key Vault

As good development practice teams should not check-in any secrets/configuration setting like DB connection strings, usernames, passwords. Instead they should be be kept in safe stores like key vault and accessed directly from your application, without much ado/difficulty. 

 If you need to configure your application with Azure key vault for reading all your application configuration keys / settings, then do the following changes in your application and it would bind the appsettings.config with the keyvault of your settings/configuration. 


Here the following settings you need to do in your code : 

using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;
using Microsoft.Extensions.Hosting;

namespace Sample.Parag.KeyVaultExample.TestWF
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
             .ConfigureAppConfiguration(async (builder) =>
             {
                 var builtConfig = builder.Build();
                 var keyVaultName = builtConfig["KeyVaultName"];
                 var azureServiceTokenProvider = new AzureServiceTokenProvider();
                 var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
                 builder.AddAzureKeyVault(
                   $"https://{keyVaultName}.vault.azure.net/", keyVaultClient, new DefaultKeyVaultSecretManager());
             })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}
Here KeyVaultName in buildConfig["KeyVaultName"], is defined in appsettings.json , and it is the name of keyvault name. 

like 
...
"KeyVaultName": "azureApp-key-vaultName",
...

Next you can start accessing any secret stored in Azure Key Vault like this : 


        public string GetFooFunc()
        {
            return this.config["SomeKeyName"];
        }

Wednesday, June 17, 2020

Visual Studio Error: Fails to launch Debugger for ASP.NET web project

Visual Studio Debugger launch error : One or more error occurred
I recently ran into the following error while trying to launch Visual studio debugger for one of my ASP.NET web project


Simple web application which does some data rendering connected to a COSMOS DB. well it was working fine for some time, and then suddenly it started failing.
The error reads :

One or more errors occurred. 

Failed to Launch Debug adapter. Additional Information may be available in the output window. 

Cannot connect to runtime process, timeout after 10000 ms - Reason : Cannot connect to target: connect ECONNREFUSED 127.0.0.1:61479



Turned out that the problem was that during the last debug session/run, the chrome instance had not died/closed (which it usually does by itself). So this time launching chrome was giving the error. Well the error is cryptic and not much helpful, unless you look it up.
so go ahead, locate your previuosly launched debug browser window and close it. And press debug again and you should be good to go.



[C#] Mutliple Concurrent Producers and Consumers pattern for a Task Queue

If you are in need of a basic concurrent Producers consumers pattern to be used in your application, here is a sample C# program to refer to...