如何解决Azure 函数在错误的位置搜索文件
我有一个 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,"somefile.txt");
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。