Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function, EF Core, Can't load ComponentModel.Annotations 4.2.0.0

Tags:

I have created several .Net Standard 2.0 libraries, tested the execution via a console application, as well as several tests - all is good.

Move over to azure function, and get the following run-time error: enter image description here

I then try to download that specific version into the API Function project: enter image description here

I'm using Visual Studio Version 15.7.0 Preview 5.0. I have updated the Azure Function to 4.7... as the console and test projects are - and those work.

Been at this a far too many hours.. so I'm hoping the resolution isn't something crazy. Ef Core 2.1.0-rc1-final is also in the mix. Using data annotations for Required, MaxLength, NotMapped.

Error in graphic says: Microsoft.EntityFrameworkCore: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0

like image 293
codeputer Avatar asked May 15 '18 04:05

codeputer


2 Answers

I would suggest running this function below once you start your Azure Function. 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;
    }

}
like image 134
Igne B Avatar answered Sep 18 '22 16:09

Igne B


I followed the instructions here:

https://codopia.wordpress.com/2017/07/21/how-to-fix-the-assembly-binding-redirect-problem-in-azure-functions/

And added the following redirect:

"BindingRedirects": "[ { "ShortName": "System.ComponentModel.Annotations", "RedirectToVersion": "4.2.1.0", "PublicKeyToken": "b03f5f7f11d50a3a" } ]"

NOTE: Its not v 4.5.0.0 ... Its actually 4.2.1.0.

like image 30
Roy Salisbury Avatar answered Sep 19 '22 16:09

Roy Salisbury