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

6

背景:

我有一个基于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 Studios构建Azure函数时,它会创建以下文件夹结构(请忽略我的可怕插图)。

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

System.Configuration.ConfigurationManager.dll 存在于 Debug/ 文件夹而不是二级 bin 文件夹中。当构建并查看相应的 dll 文件时,我可以看到它被填充到二级 bin 文件夹中,但是在 Azure Function 项目启动之前立即被删除。

现在,当我关闭本地运行,并将 System.Configuration.ConfigurationManager.dll 添加回二级 bin 文件夹中时,一切都工作得非常完美!

实际问题(请帮忙!)

问题1:有没有一种方法可以明确告诉 Visual Studio 将 dll 输出到二级 bin 文件夹中?我已经尝试过以下操作...

  • GeneratePathProperty="true"
  • COPY_PASS0
  • 所有的 stackoverflow 问题

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

感谢您的协助!


我已经浪费了一整天的时间在这个令人困惑的行为上。已经过去两年了,但问题仍然存在,除非有人知道更好的解决方法? - Andrew
2个回答

10

4
应将此设置添加到 <PropertyGroup> 部分嵌套的 csproj 文件中。 - Jared Stroebele
它对我解决与Newtonsoft.Json相关的版本问题非常有效。 - JCallico

0

感谢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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接