Sam
Sam

Reputation: 30388

Passing Configuration and Initializing a Client in Azure Functions w/ Dependency Injection

I'm creating a new Azure Functions with a Queue trigger. A key requirement is for me to use my existing class libraries that I created in my ASP.NET Core app so that I can access my Repository methods. I also have my own clients that handle communication with some third party services.

I need help with creating instances of my clients and passing configuration to them which is IConfiguration.

Here's what my Startup.cs looks like in my Azure Functions project:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyApp.Infrastructure.Clients;
using MyApp.Infrastructure.Interface;
using MyApp.Infrastructure.Repositories;

[assembly: FunctionsStartup(typeof(MyTestFunction.Startup))]
namespace MyTestFunction
{
    public class Startup : FunctionsStartup
    {
        public IConfiguration Configuration { get; }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton(new MyApp.Infrastructure.Clients.MyClient123(Configuration));
            builder.Services.AddTransient<ICommunicationsRepository, CommunicationsRepository>();
        }
    }
}

In my ASP.NET Core app's Startup.cs, I do have a constructor that handles the configuration -- see below -- but not sure how to handle this Azure Functions.

public Startup(IConfiguration configuration)
{
     Configuration = configuration;
}

public IConfiguration Configuration { get; }

Upvotes: 0

Views: 1313

Answers (1)

Sam
Sam

Reputation: 30388

Looks like this is not a recommended approach: https://github.com/Azure/azure-functions-host/issues/4464#issuecomment-513017446

I've decided to update my class libraries in my ASP.NET Core app so that I use Azure KeyVault in both my API and Azure Functions apps.

Upvotes: 1

Related Questions