ExecutionContext 对通过 IoC 的非函数方法为空,替代 ExecutionContext.FunctionAppDirectory [英] ExecutionContext is null to non-function methods via IoC, alternative to ExecutionContext.FunctionAppDirectory

查看:12
本文介绍了ExecutionContext 对通过 IoC 的非函数方法为空,替代 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆