在 Azure 函数中找不到注释 System.ComponentModel.Annotations

Cannot find annotations System.ComponentModel.Annotations in Azure Function

我有一个简单的 Azure 函数,它只需要将一些 json 反序列化为具有注释的对象

我收到错误

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

我该如何解决这个问题?这是使用 Azure Functions v3 的 .NET Core 3.1

我正在使用一个新的项目模板,所以这是 Azure Functions

鉴于这是一个 Azure 函数,我不确定如何实施涉及破解程序集绑定重定向的修复程序,其中 appsettings.json 文件不支持 .config

我有3种方法。,

方式-1 这是 .NET 5 的进程外函数的问题。因此,您可以将项目从 .NET Core 3.1 升级到 .NET 5.0,或者将所有依赖包降级到 3.1。 你可以找到更多关于 here

方式二 如果这不起作用,请在您的 azure 函数中尝试 运行 this 。它会将任何程序集重定向到现有版本。

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;
   }
}

方式三 尝试将以下代码添加到项目的 .csproj 文件中:

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

这会强制构建过程在输出目录中创建一个包含所需绑定重定向的 .dll.config 文件。

您可以在讨论类似相关问题的 SO 线程 , 中找到更多信息。