Search code examples
azureannotationsazure-functions

Cannot find annotations System.ComponentModel.Annotations in Azure Function


I have a simple Azure function that just needs to deserialize some json into an object that has annotations

I get the error

'System.ComponentModel.Annotations, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

How can I fix this? This is .NET Core 3.1 using v3 of Azure Functions

I am using a new project template so this is Azure Functions

Im not sure how to implement fixes that involve hacking around with assembly binding redirects given that this is an Azure Function, which has appsettings.json file not support for .config


Solution

  • I have 3 ways for this.,

    WAY -1 This is an issue with out of process functions with .NET 5. So, you can either upgrade your project from .NET Core 3.1 to .NET 5.0 or downgrade all dependent packages to 3.1. you can find more on that here

    WAY-2 If this doesn't work try running this in your azure functions . It will redirect any assembly to an existing version.

    public class FunctionsAssemblyResolver
    {
      public static void RedirectAssembly()
       {
           var list = AppDomain.CurrentDomain.GetAssemblies().OrderByDescending(a => a.FullName).Select(a => a.FullName).ToList();
           AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
       }
    
       private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
       {
           var requestedAssembly = new AssemblyName(args.Name);
           Assembly assembly = null;
           AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
           try
           {
               assembly = Assembly.Load(requestedAssembly.Name);
          }
          catch (Exception ex)
           {
           }
           AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
           return assembly;
       }
    }
    

    WAY-3 Try adding the following code to the .csproj file of your project:

    <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
    
    

    This forces the build process to create a .dll.config file in the output directory with the needed binding redirects.

    You can find more information in the SO Threads Thread1 , Thread2 which discusses on similar related issue.