0
votes

ExecutionContext is available to functon parameters.

However, it is not available to other methods via dependency inject, including Functions' constructor, like below:

    public class FunctionClass
    {   

        IOtherClass _otherclass;
       public FunctionClass(ExecutionContext  context,  //excetpion
                          IOtherClass otherclass)  //excetpion
       {
                 _otherclass = IOtherClass otherclass
       }

     [FunctionName("Car")]
        public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
        HttpRequest req, ExecutionContext  context)
        {     
          }
    }

     public class OtherClass:IOtherClass
    {   
       public OtherClass(ExecutionContext  context)  //excetpion
       {}
    }

I need access to ExecutionContext.FunctionAppDirectory, but don't want to pass ExecutionContext around, because want to use IoC instead.

Is there an alternative way to get the value of ExecutionContext.FunctionAppDirectory?

VS 2017

Azure Functons 2.x

2
ExecutionContext is only available in the scope of a request. It wont be available as yet in the constructor for injection when the function class is initialized.. - Nkosi
Is there another way to get the result of ExecutionContext.FunctionAppDirectory? - Pingpong
Depends on where you want to access it from. If accessed from the function method then it will be available in the context. - Nkosi
Where else is it available? - Pingpong
What about within the ctor of OtherClass in OP? - Pingpong

2 Answers

0
votes

Based on current documentation, ExecutionContext is only available in the scope of a request when the function method is being invoked.

[FunctionName("Car")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
    HttpRequest req, 
    ExecutionContext context //<--
) {

    var path = context.FunctionAppDirectory;

    //...
}

It wont be available as yet in the constructor for injection when the function class is initialized.

0
votes

We can use ExecutionContextOptions to get application folder:

public class FunctionClass
  private ExecutionContextOptions context;

  public FunctionClass(IOptions<ExecutionContextOptions> executionContext) {
    this.context = executionContext.Value;

    var path = Path.GetFullPath(Path.Combine(context.AppDirectory, "extra.json"));

  }
}

Note: The above works using VS 2019 and Azure Functions 3.x

See: