gpt4 book ai didi

Azure Function 在错误位置搜索文件

转载 作者:行者123 更新时间:2023-12-02 06:25:00 26 4
gpt4 key购买 nike

我有一个 Azure 函数 (C# v3),它使用位于 .\Assets\somefile.txt 的本地文本文件。 。该文件具有属性 Copy always在我的本地计算机上运行时可以正确看到。但是,当部署到 Azure Function 应用程序时,该文件预计位于 C:\home\site\wwwroot\Assets\somefile.txt但正在运行的函数看不到它并写入错误日志消息 Could not find a part of the path 'C:\Program Files (x86)\SiteExtensions\Functions\3.1.3\32bit\Assets\somefile.txt'.

如何使函数能够查看文件,而不需要硬编码文件的完整路径(无论如何,它在我的本地计算机上是不同的,我想了解问题的实际根本原因)。

最佳答案

有几种方法可以获取函数执行根路径。一是添加ExecutionContext parameter在您的函数方法中并读取 FunctionAppDirectory 值。

public static string Run(TimerInfo timer, ExecutionContext context)
{

var executionRoot = context.FunctionAppDirectory;
var filePath = Path.Combine(executionRoot, "Assets", "somefile.txt");

...
}

或者从 HOME 环境变量构建基本路径:

public static string Run(TimerInfo timer)
{

var executionRoot = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
var filePath = Path.Combine(executionRoot, "Assets", "somefile.txt");

...
}

关于Azure Function 在错误位置搜索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68477244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com