对于通过IoC进行的非功能方法,ExecutionContext为null,替代ExecutionContext.FunctionAppDirectory [英] ExecutionContext is null to non-function methods via IoC, alternative to ExecutionContext.FunctionAppDirectory
问题描述
ExecutionContext可用于功能参数.
ExecutionContext is available to functon parameters.
但是,它不能通过依赖项注入用于其他方法,包括Function的构造函数,如下所示:
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 Functons 2.x
Azure Functons 2.x
推荐答案
基于当前文档,ExecutionContext
仅在调用函数方法时在请求范围内可用.
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.
这篇关于对于通过IoC进行的非功能方法,ExecutionContext为null,替代ExecutionContext.FunctionAppDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!