I recently merged 2 projects (Service made of Azure Functions & Common libraries) and I had to upgrade a few dependencies to fix compliance and compatibility issues.
The function app gets deployed correctly, but I get this when the function is invoked:
Exception while executing function: ActiveTestEventGenV2 Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
I updated all the projects in my solution to target net6.0
and I am using Azure Functions V4.
I have a vague idea what is happening; I have a direct dependency on System.Text.Json
and I also have a transitive dependency on the same, but with a different version.
I do not understand the issue completely. Please link any documentation, blog, or video that explains the issue. I have linked a blog below, but I don't think this blog applies to my error as I am not targetting older dotnet versions - all my projects are targetting net6.0
.
Here's all I have tried to resolve the issue:
I read through this blog and added the RestoreProjectStyle, but wasn't able to resolve the issue: https://www.hanselman.com/blog/referencing-net-standard-assemblies-from-both-net-core-and-net-framework
Explicitly added dependency to each csproj file, didn't work.
Added explicit dependency in solution's common Directory.Build.targets, didn't work.
Added a build script that automatically copies the dll to publish directory. The dll is copied, but it didn't resolve the issue. The function app still ran into the above issue.
I could not find how do I set "Copy Local" (Basically forcing copying local dlls to final) in Visual Studio 2022.
I set the option true to csproj, does not work!
Used Visual Studio to try and consolidate dependencies, it didn't show multiple versions of System.Text.Json
.
Can I please get some help on this?
As I write this question, I am planning to run dependency analyzers and see which version of System.Text.Json
are being used by my solution.
This is related to Azure Function cleaning up dlls that it thinks are unnecessary to reduce the deployment size.
What seems to have worked is adding <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
flag in PropertyGroup - please note that this might increase your function size and increase your cold start time. I don't really care about it much because of my usecase, but there's another way to whitelist dependencies. Here's a blog post explaining this: https://bryanknox.github.io/2022/07/15/functionsskipcleanoutput-and-functionspreserveddependencies.html.