Azure Functions binding redirect

前端 未结 6 597
南笙
南笙 2020-11-30 04:39

Is it possible to include a web.config or app.config file in the azure functions folder structure to allow assembly binding redirects?

6条回答
  •  眼角桃花
    2020-11-30 05:38

    Inspired by the accepted answer I figured I'd do a more generic one which takes into account upgrades as well.

    It fetches all assemblies, orders them descending to get the newest version on top, then returns the newest version on resolve. I call this in a static constructor myself.

    public static void RedirectAssembly()
    {
        var list = AppDomain.CurrentDomain.GetAssemblies()
            .Select(a => a.GetName())
            .OrderByDescending(a => a.Name)
            .ThenByDescending(a => a.Version)
            .Select(a => a.FullName)
            .ToList();
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            var requestedAssembly = new AssemblyName(args.Name);
            foreach (string asmName in list)
            {
                if (asmName.StartsWith(requestedAssembly.Name + ","))
                {
                    return Assembly.Load(asmName);
                }
            }
            return null;
        };
    }
    

提交回复
热议问题