8

When using the NuGet.Protocol NuGet Package in Azure Functions, I get the following error: System.Private.CoreLib: Could not load file or assembly.

[4/18/2020 8:51:43 AM] The 'Function1' function is in error: Unable to load one or more of the requested types.
[4/18/2020 8:51:43 AM] Could not load file or assembly 'NuGet.Protocol, Version=5.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
[4/18/2020 8:51:43 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. System.Private.CoreLib: Could not load file or assembly 'NuGet.Protocol, Version=5.5.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

I confirmed that the NuGet.Protocol.dll file exists in the bin output.

enter image description here

Why can Azure Functions not find NuGet.Protocol.dll?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123

1 Answers1

21

This is a known-problem with Azure Functions, introduced in Microsoft.NET.Sdk.Functions v3.0.4, and still present today (v3.0.11).

To prevent Azure Functions from removing this library, add the following to your CSPROJ:

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

Here is an example of how I use it in my GitTrends app: https://github.com/brminnick/GitTrends/blob/22d748fc72452dcd39bb3866e30f339827ded3dd/GitTrends.Functions/GitTrends.Functions.csproj#L10

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
  • 1
    The solution for in-process Azure Functions continues to be undocumented. I wrote a blog post that might help at: https://bryanknox.github.io/2022/07/15/functionsskipcleanoutput-and-functionspreserveddependencies.html – Bryan Knox May 02 '23 at 22:25