0

How I access IConfiguration object in classes of Azure Functions project. I am new to this type of code, but I need to access IConfiguration in some of the class files defined in the project.

Ex: in the TestService, also in SampleService which is not listed Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
   var config = builder.GetContext().Configuration;
   builder.Services.AddTransient<ITestService, TestService>();
}
Rag Pudi
  • 33
  • 1
  • 7
  • 1
    Does this help?: https://stackoverflow.com/questions/59474070/how-to-inject-or-use-iconfiguration-in-azure-function-v3-with-dependency-injecti – SunnyDark Dec 10 '21 at 22:55
  • @SunnyDark Thanks for quick response. Let me check the link. – Rag Pudi Dec 10 '21 at 22:59
  • @DeepDave-MT not yet Dave. If you know something please post here. It will help me as well as others. Thanks – Rag Pudi Dec 13 '21 at 14:10

1 Answers1

0

Thankyou SunnyDark. Posting your suggestion as an answer so that it will be helpful for other community members who face similar kind of issues.

IConfiguration is added to the service collection by default as part of the start up, Below is the example for IConfiguration

public static class BootstrapCosmosDbClient {

    private static event EventHandler initializeDatabase = delegate { };

    public static IServiceCollection AddCosmosDbService(this IServiceCollection services) {

        Func<IServiceProvider, ICosmosDbService> factory = (sp) => {
            //resolve configuration
            IConfiguration configuration = sp.GetService<IConfiguration>();
            //and get the configured settings (Microsoft.Extensions.Configuration.Binder.dll)
            CosmosDbClientSettings cosmosDbClientSettings = configuration.Get<CosmosDbClientSettings>();
            string databaseName = cosmosDbClientSettings.CosmosDbDatabaseName;
            string containerName = cosmosDbClientSettings.CosmosDbCollectionName;
            string account = cosmosDbClientSettings.CosmosDbAccount;
            string key = cosmosDbClientSettings.CosmosDbKey;

            CosmosClientBuilder clientBuilder = new CosmosClientBuilder(account, key);
            CosmosClient client = clientBuilder.WithConnectionModeDirect().Build();
            CosmosDbService cosmosDbService = new CosmosDbService(client, databaseName, containerName);

            //async event handler
            EventHandler handler = null;
            handler = async (sender, args) => {
                initializeDatabase -= handler; //unsubscribe
                DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
                await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
            };
            initializeDatabase += handler; //subscribe
            initializeDatabase(null, EventArgs.Empty); //raise the event to initialize db

            return cosmosDbService;
        };
        services.AddSingleton<ICosmosDbService>(factory);
        return service;
    }
}

For Further information check the SO and Web Application.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15