4

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.

Nadeem Tabbaa
  • 119
  • 3
  • 9

1 Answers1

3

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.

JanneP
  • 577
  • 4
  • 12
  • yes, very good answer, and i know that i could do that, but the issue is the function App doesnot have a startup class as program.cs for example, it is a serverless type of application. I am not sure where should i add services.AddSinleton – Nadeem Tabbaa Oct 08 '18 at 06:57
  • 1
    You would need to implement DI as a custom solution, such as https://mcguirev10.com/2018/04/03/service-locator-azure-functions-v2.html, or just skip it altogether(that's why I marked it optional). You could just create, or map with AutoMapper etc., a new instance of type FunctionAppExecutionContext from ExecutionContext and have the interface as parameter type in helper classes, whether static or not. – JanneP Oct 08 '18 at 14:05