我通过集成的 azure 函数扩展将 vscode 与我的 azure 帐户链接,函数采用 C# 语言。通过“部署到函数应用程序...”按钮,函数在本地和云上按预期工作。我的问题是,当某些函数运行时,如何将部署中包含的一些文件用作资源(例如文本文件)?我可以看到如何使用 Visual Studio 来完成此操作,但我还没有弄清楚如何使用 Visual Studio 代码和 azure 函数扩展来完成此操作。
最佳答案
可能是更好的方法,但如果您一直在 vscode 中使用 nuget 包管理器等插件,您只需将 CopyToOutputDirectoty 元素添加到它将创建的 xxxxx.csproj 文件中即可 (它使用与 Visual Studio 中相同的方法)
例如,我手动添加了文件中的最后一个条目
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.17.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
<None Update="data/sample.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
然后使用类似的方法在 C# 中读取文件
string responseMessage = File.ReadAllText(Path.GetFullPath(Path.Combine(context.FunctionDirectory, "..\\data\\sample.json")));
关于azure - 如何在 vscode azure 函数中包含资源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63567464/