ExecutionContext 对通过 IoC 的非函数方法为空,替代 ExecutionContext.FunctionAppDirectory [英] ExecutionContext is null to non-function methods via IoC, alternative to ExecutionContext.FunctionAppDirectory
问题描述
ExecutionContext 可用于函数参数.
ExecutionContext is available to functon parameters.
但是,它不适用于其他方法通过依赖注入,包括 Functions 的构造函数,如下所示:
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
{}
}
我需要访问 ExecutionContext.FunctionAppDirectory
,但不想传递 ExecutionContext,因为我想改用 IoC.
I need access to ExecutionContext.FunctionAppDirectory
, but don't want to pass ExecutionContext around, because want to use IoC instead.
有没有其他方法可以获取 ExecutionContext.FunctionAppDirectory
的值?
Is there an alternative way to get the value of ExecutionContext.FunctionAppDirectory
?
VS 2017
Azure 函数 2.x
Azure Functons 2.x
推荐答案
我们可以使用ExecutionContextOptions
来获取应用文件夹:
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"));
}
}
注意:以上工作使用 VS 2019 和 Azure Functions 3.x
Note: The above works using VS 2019 and Azure Functions 3.x
见:
这篇关于ExecutionContext 对通过 IoC 的非函数方法为空,替代 ExecutionContext.FunctionAppDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!