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:
I then try to download that specific version into the API Function project:
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
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;
}
}
Great, that worked! However, i suppose, this would have an effect on every function within the same function app i suppose?
I am not clear as to how you are running this. I have tried it as the first line of a functions
Run(...)
method, and in a static constructor on the functions class, but it doesn't seem to get called during the build?Yes, that's exactly what I'm doing - running it on Run(...) or the constructor. It is not supposed to be called during build, it's a runtime issue, so it gets only called then.
Many thanks; worked for me as well.
Note: This will only redirect the packages installed in the project in which the method is called. For example, if you call
FunctionsAssemblyResolver.RedirectAssembly()
in a "Services" project, it won't change the packages in an "Infrastructure" project, even if they're added as a reference. Having said that, THANK YOU! I've tried so many solutions for this dumb problem and this is the one that worked.