【问题标题】:Cannot find annotations System.ComponentModel.Annotations in Azure Function在 Azure 函数中找不到注释 System.ComponentModel.Annotations
【发布时间】:2021-06-21 12:52:54
【问题描述】:

我有一个简单的 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,因此我不确定如何实施涉及破解程序集绑定重定向的修复

【问题讨论】:

    标签: azure annotations azure-functions


    【解决方案1】:

    我有 3 种方法。

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

    WAY-2 如果这不起作用,请尝试在您的 azure functions 中运行它。它将任何程序集重定向到现有版本。

    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 尝试将以下代码添加到项目的 .csproj 文件中:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多