Azure Functions binding redirect

前端 未结 6 602
南笙
南笙 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:22

    Here's an alternate solution for when you want the exact version of a particular assembly. With this code, you can easily deploy the assemblies that are missing:

    public static class AssemblyHelper
    {
        //--------------------------------------------------------------------------------
        /// 
        /// Redirection hack because Azure functions don't support it.
        /// How to use:  
        ///     If you get an error that a certain version of a dll can't be found:
        ///         1) deploy that particular dll in any project subfolder 
        ///         2) In your azure function static constructor, Call 
        ///             AssemblyHelper.IncludeSupplementalDllsWhenBinding()
        ///         
        /// This will hook the binding calls and look for a matching dll anywhere 
        /// in the $HOME folder tree.  
        /// 
        //--------------------------------------------------------------------------------
        public static void IncludeSupplementalDllsWhenBinding()
        {
            var searching = false;
    
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                // This prevents a stack overflow
                if(searching) return null;
                var requestedAssembly = new AssemblyName(args.Name);
                searching = true;
    
                Assembly foundAssembly = null;
                try
                {
                    foundAssembly = Assembly.Load(requestedAssembly);
                }
                catch(Exception e)
                {
                    Debug.WriteLine($"Could not load assembly: {args.Name} because {e.Message}");
                }
    
                searching  = false;
    
                if(foundAssembly == null)
                {
                    var home = Environment.GetEnvironmentVariable("HOME") ?? ".";
    
                    var possibleFiles = Directory.GetFiles(home, requestedAssembly.Name + ".dll", SearchOption.AllDirectories);
                    foreach (var file in possibleFiles)
                    {
                        var possibleAssembly = AssemblyName.GetAssemblyName(file);
                        if (possibleAssembly.Version == requestedAssembly.Version)
                        {
                            foundAssembly = Assembly.Load(possibleAssembly);
                            break;
                        }
                    }
                }
    
                return foundAssembly;
            };
        }
    }
    

提交回复
热议问题