How to inject or use IConfiguration in Azure Function V3 with Dependency Injection when configuring a service

前端 未结 4 1881
挽巷
挽巷 2021-02-02 12:46

Normally in a .NET Core project I would create a \'boostrap\' class to configure my service along with the DI registration commands. This is usually an extension method of

4条回答
  •  迷失自我
    2021-02-02 13:15

    The newly released version 1.1.0 of Microsoft.Azure.Functions.Extensions allows you to do the following:

    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var configuration = builder.GetContext().Configuration;
            builder.Services.AddCosmosDbService(configuration);
        }
    }
    

    Unfortunately it still does not support async configuration so you will still have to block waiting for the task to finish or use the trick described by @Nkosi

提交回复
热议问题