如何将文件复制到 Azure Function bin 文件夹中?

How do you copy files into Azure Function bin folder?

提问人:Patrick Le 提问时间:9/18/2020 更新时间:4/25/2023 访问量:2525

问:

背景:

我有一个在 netcoreapp3.1 和 azure 版本 v3 中运行的 Azure 函数 C# 项目。这是 csproj 的一个片段......

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AssemblyName>someproj</AssemblyName>
    <RootNamespace>someproj</RootNamespace>
    <Product>someproduct</Product>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <RunPublishAfterBuild>true</RunPublishAfterBuild>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.15.0" />
    <PackageReference Include="Microsoft.Applications.Telemetry.Server">
      <Version>1.1.264</Version>
      <NoWarn>NU1701</NoWarn>
    </PackageReference>
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0-preview.8.20407.11">
      <NoWarn>NU1701</NoWarn>
    </PackageReference>
  </ItemGroup>
</Project>

运行后异常:

'Could not load file or assembly 'System.Configuration.ConfigurationManager, 
 Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one 
 of its dependencies. The system cannot find the file specified.'

疑似原因:

问题是通过 Visual Studio 生成 azure 函数时,它会创建以下文件夹结构。(请原谅我可怕的插图)

bin/
 | - Debug/
 | | -bin / 
 | | | runtimes/
 | | - function1/
 | | | - ...
...
---

System.Configuration.ConfigurationManager.dll 位于 Debug/ 文件夹中,而不是二级 bin 文件夹中。在生成并盯着相应的 dll 文件时,我可以看到它正在第二个 bin 文件夹中填充,但后来在 Azure 函数项目启动之前被删除。

现在,当我关闭本地运行时,将System.Configuration.ConfigurationManager.dll添加回第二个bin文件夹中,一切正常!

实际问题(请帮忙!

问题 1:有没有办法显式告诉 Visual Studio 将 dll 输出到第二个 bin 文件夹中?我已经尝试过以下方法......

  • GeneratePathProperty=“true”
  • COPY_PASS0
  • 所有 stackoverflow

问题 2:有没有更好的方法来引用这个必需的 dll?我已经尝试过 nuget 控制台,但没有运气。

感谢大家的帮助!

C# ASP.net-core azure-functions system.configuration netcoreapp3.1

评论

0赞 Andrew 4/11/2023
我花了一整天的时间在这种令人困惑的行为上。已经两年多了,这仍然是一个问题,除非有人碰巧知道更好的解决方法?

答:

10赞 Forrest Dillaway 9/18/2020 #1

众所周知,由 Target _FunctionsBuildCleanOutput 运行的 Task、RemoveRuntimeDependencies 会过于激进地清除程序集

解决方法是通过设置完全关闭任务(并接受更大的部署):

<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

跟踪工作项以实现更精细的切换:https://github.com/Azure/azure-functions-host/issues/5894

评论

4赞 Jared Stroebele 11/26/2020
此设置应添加到嵌套在该部分中的 csproj 文件中。<PropertyGroup>
0赞 JCallico 12/11/2020
它为我解决了与 Newtonsoft.Json 相关的版本问题
0赞 Kevin M. Lapio 4/25/2023 #2

感谢 Forrest Dillaway 提供的全有或全无解决方法。

由于我在这里遇到了同样的问题,因此我想添加该答案中引用的“粒度切换”解决方案,以节省人们单击几个链接的过程。

若要保留单个依赖项,请按以下格式将它们添加到项目文件中:

  <ItemGroup>
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.dll" />
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.Extensions.dll" />
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.Features.dll" />
  </ItemGroup>

编号: https://github.com/Azure/azure-functions-host/pull/6849