azure - 如何在 vscode azure 函数中包含资源文件?

标签 azure visual-studio-code

我通过集成的 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/

相关文章:

azure - Azure Function 中的 StackExchange.Redis 问题(ResetNonConnected)

c# - sql 选择顶部 X 到 X+N

c# - context.Database.ExecuteSqlCommand - 错误代码 701 - Microsoft Azure

python - 如何在 Azure ML Studio 中保存 ipython 笔记本中的数据集?

azure - 如何从 VS Code 发布到 Azure 中的开发槽

go - 如何在 Visual Studio Code 中保存时运行 `go fmt`?

typescript - 从自动导入中排除模式的选项不起作用?

azure - 将文件从 SFTP 复制到 Blob 存储时并行化 Azure 逻辑应用执行

visual-studio-code - VSCode 在编辑 Markdown 和预览 Pane 时跳动打开

node.js - 如何从Node.js中内置的VSCode运行js文件?