1
votes

I want to create an Azure Durable Function that will download a CSV from the Internet and based on the data in this file, it will update my database using EntityFramework.

I set up the simple startup function that is triggered with TimeTrigger. This function is responsible for starting the orchestrator. The orchestrator executes multiple activities in parallel. There are around 40000 work items to be processed, so that's the number of activities that are triggered by orchestrator. Some of these activities will need to update the database (insert/update/delete rows). For this I need a database connection. I can configure DI in the StartUp in the following way:

public override void Configure(IFunctionsHostBuilder builder)
        {
            var connectionString = Environment.GetEnvironmentVariable("DefaultConnection");
            builder.Services.AddDbContext<SqlContext>(options => options.UseSqlServer(connectionString));
            builder.Services.AddScoped<IDbContext, SqlContext>();
        }
    }

However all my functions (orchestrator, activity function, etc.) are static and reside in a static class. I haven't seen any example where durable functions were defined in a non-static class and I had all kinds of problems when I tried that myself, so I assumed they must be static without diving too much into it.

I do not know how to pass my DbContext object to the Activity function, so it can update the data in the database when needed.

How should I resolve it?

1

1 Answers

1
votes

I want to create an Azure Durable Function that will download a CSV from the Internet and based on the data in this file, it will update my database using EntityFramework.

Configure DI in the StartUp in the following way:

public override void Configure(IFunctionsHostBuilder builder) {
    var connectionString = Environment.GetEnvironmentVariable("DefaultConnection");

    builder.Services.AddDbContext<IDbContext, SqlContext>(options => 
        options.UseSqlServer(connectionString)); //To inject DbContext

    builder.Services.AddHttpClient(); //To inject HttpClient
}

Ensure you host your function app on Azure Functions Runtime V3+ so the class and methods don’t have to be static.

This will allow regular classes that have non-static constructors with injectable arguments

public class MyFunction {
    private readonly HttpClient httpClient;
    private readonly IDbContext dbContext;

    //ctor
    public MyFunction(IHttpClientFactory factory, IDbContext dbContext) {
        httpClient = factory.CreateClient();
        this.dbContext = dbContext;
    }

    [FunctionName("Function_Name_Here")]
    public async Task Run(
        [OrchestrationTrigger] IDurableOrchestrationContext context) {

        // ... access dependencies here

    }

    // ... other functions, which can include static, but they wont
    // have access to the instance fields.
}

This series of articles might be of some assistance to you

A Practical Guide to Azure Durable Functions — Part 2: Dependency Injection