How do you copy files into Azure Function bin folder?

2.7k Views Asked by At

Background:

I have an Azure Function C# Project running in netcoreapp3.1 and azure version v3. Here's a snippet of the 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>

Exception After Run:

'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.'

Suspected Reason:

The problem is when building the azure function via Visual Studios, it creates the following folder structure. (Forgive my horrible illustration)

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

System.Configuration.ConfigurationManager.dll lives in the Debug/ folder and NOT the second level bin folder. When building and staring at the respective dll file, I can see it being populating in the second bin folder but later gets deleted right before the Azure Function Projects starts up.

Now when I turn off the local run, add in the System.Configuration.ConfigurationManager.dll back into the second bin folder, everything works perfectly fine!

Actual Questions (please help!)

Question 1: Is there a way to explicitly tell visual studio to output the dll into the second bin folder? I've already tried the following...

  • GeneratePathProperty="true"
  • COPY_PASS0
  • all the stackoverflows

Question 2: Is there a better way to reference this required dll? I've already tried the nuget console and no luck.

Thanks for the assistance y'all!

2

There are 2 best solutions below

2
On BEST ANSWER

Task, RemoveRuntimeDependencies, run by Target, _FunctionsBuildCleanOutput, is known to purge assemblies too aggressively.

The workaround is to turn off the task entirely (and accept larger deployments) by setting:

<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

Tracking work item for more granular toggles: https://github.com/Azure/azure-functions-host/issues/5894

0
On

Thanks to Forrest Dillaway for the all-or-nothing workaround.

Since I landed here for the same problem, I wanted to add the "granular toggles" solution referenced in that answer to save people clicking a couple links.

To preserve individual dependencies, add them to your project file in the following format:

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

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