Reputation: 8009
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
Upvotes: 2
Views: 1843
Reputation: 815
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:
Upvotes: 2
Reputation: 247163
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.
Upvotes: 0