2

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

Pingpong
  • 7,681
  • 21
  • 83
  • 209
  • `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 Mar 11 '19 at 01:49
  • Is there another way to get the result of ExecutionContext.FunctionAppDirectory? – Pingpong Mar 11 '19 at 01:59
  • Depends on where you want to access it from. If accessed from the function method then it will be available in the context. – Nkosi Mar 11 '19 at 02:00
  • Where else is it available? – Pingpong Mar 11 '19 at 02:03
  • What about within the ctor of OtherClass in OP? – Pingpong Mar 11 '19 at 02:04
  • You'll end up with the same problem. When these classes are initialized for injection the context has not been created as yet so it is not able to be injected, – Nkosi Mar 11 '19 at 02:05

2 Answers2

2

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:

minus one
  • 642
  • 1
  • 7
  • 28
0

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.

Nkosi
  • 235,767
  • 35
  • 427
  • 472