Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ExecutionContext Globally in Azure Function App

I have function Apps, and in a helper class that is going to prepare a file and store it for me i want to read the project file

I tried many ways, such as (AppDomain, AppContext, .... etc) and many others, and as it is serverless, the program.cs is at different directory from the function where it is running. what i want is the WebRoot directory.

i found this https://github.com/Azure/azure-functions-host/wiki/Retrieving-information-about-the-currently-running-function and this is giving me the correct path i want, the wwwroot by using FunctionAppDirectory.

the issue is I need to pass the ExecutionContext to the run function, and then i need to pass it to the Helper class, I cannot read it directly from the helper classes. and i have this in multiple places.

How i can get this globally across the application/Classes.

thanks.

like image 951
Nadeem Tabbaa Avatar asked Sep 14 '25 04:09

Nadeem Tabbaa


1 Answers

One way to solve your problem would be to

  1. Define a global interface:

    public interface IExecutionContext
    {
        Guid InvocationId { get; set; }
        string FunctionName { get; set; }
        string FunctionDirectory { get; set; }
        string FunctionAppDirectory { get; set; }
    }
    
  2. Add implementation for Function App:

    public class FunctionAppExecutionContext : ExecutionContext, IExecutionContext
    {
    }
    
  3. Add DI configuration for Function App(optional):

    services.AddSingleton<IExecutionContext, FunctionAppExecutionContext>();

Then, in your helper classes, use the interface to hide the implementation. Which, in this case, is Functions SDKs ExecutionContext. Repeat the class implementation steps to implement classes for other libraries.

like image 64
JanneP Avatar answered Sep 16 '25 18:09

JanneP