我在使用 Azure 函数时遇到问题,看到 bin 内创建了另一个 bin 文件夹。所以文件夹结构就像这样 bin -> net461 -> bin -> dll。但是,项目中的任何其他文件(json、xml)都会被复制到 bin -> net461 。由于这种引用文件(json,xml)在代码中变得困难。我不需要另一个 bin 文件夹。如何解决这个问题?
最佳答案
I try to read the file something like this File.ReadAllText(@"FolderName\FileName.json") but when function executes it gives error - Could not find a part of the path 'D:\Windows\system32\FolderName\FileName.json'.
我检查了这个问题,发现您的代码可以在本地正确运行,但在部署到azure端后失败。您可能需要显式设置绝对路径来读取 json 文件。您可以利用 Microsoft.Azure.WebJobs.ExecutionContext.FunctionDirectory 获取当前函数目录(例如 D:\home\site\wwwroot\<FunctionName>
表示 Azure 端, C:\Users\xxx\FunctionApps\bin\Debug\net461\<FunctionName>
表示本地端)。详情可关注Retrieving information about the currently running function .
读取 json 文件的代码如下所示:
File.ReadAllText(Path.Combine(Directory.GetParent(executionContext.FunctionDirectory).FullName,@"helloworld.json"));
关于.net - Azure Functions 在 bin 内创建另一个 bin 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50302293/