Azure Function V3 configuration with DI

Regular Visitor

I have a Azure Function with 2 triggers:

namespace FunctionApp
{
    public class Functions
    {
        private readonly IService service;
 
        public Functions(IService service)
        {
            this.service = service;
        }
 
        [FunctionName("Function1")]
        public void Function1([BlobTrigger("funcone-requests/{name}")] Stream myBlob, string name)
        {
            this.service.DoWork();
        }
 
        [FunctionName("Function2")]
        public void Function2([BlobTrigger("functwo-requests/{name}")] Stream myBlob, string name)
        {
            this.service.DoWork();
        }
    }
}

I’m registering IService in my Startup class like so:

[assembly: FunctionsStartup(typeof(FunctionApp.Startup))]
namespace FunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddScoped<IService, Service>();
        }
    }
}

 I need to inject a different configuration in the Service class depending on which trigger that is calling DoWork(). How can I achieve this using DI? Or a recommended alternative approach?

public interface IService
{
    void DoWork();
}
 
public class Service : IService
{
    public Service(/*Configuration to be injected depends on calling function*/)
    { }
 
    public void DoWork()
    { }
}

 

Configuration extract:

{
  "Values": {
    "Function1:Server": "Function1_Server",
    "Function1:Port": "Function1_Port",
    "Function2:Server": "Function2_Server",
    "Function2:Port": "Function2_Port"
  }
}
public class Config
{
    public string Server { get; set; }
    public string Port { get; set; }
}
 
0 Replies