运行 一段时间后,Azure Function 无法加载程序集

Azure Function fails to load assemblies after running fine for a while

我有一个函数可以侦听频繁插入的 blob 存储,尽管有时很少足以让该函数闲置。

它的设置方式是在 bin 文件夹中包含一些自定义 DLL,而实际函数本身只是在其中一个程序集中调用 运行 方法。这很好用。大多数时候。

我的问题是,如果我让它 运行 一段时间,它会突然无法加载我的自定义程序集,并出现以下错误消息:

Exception while executing function: Functions.EventHandlerFunctionMicrosoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.EventHandlerFunction ---> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ---> System.TypeLoadException : Could not load type 'MyAssembly.MyFunctions.EventHandlerFunction' from assembly 'MyAssembly.MyFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.   at async Submission#0.Run(Stream blob,String name,TraceWriter log) at  : 19   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)   at Submission#0.Run(Stream blob,String name,TraceWriter log)    End of inner exception   at System.RuntimeMethodHandle.InvokeMethod(Object target,Object[] arguments,Signature sig,Boolean constructor)   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object[] parameters,Object[] arguments)   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object[] parameters,CultureInfo culture)   at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.InvokeCore(Object[] parameters,FunctionInvocationContext context)   at async Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters)   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.InvokeAsync[TReflected](Object[] arguments)   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,Object[] invokeParameters,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance)   at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,IReadOnlyDictionary`2 parameters,TraceWriter traceWriter,CancellationToken…

正如您从堆栈跟踪中看到的那样,我的程序集(重命名为 MyAssembly.MyFunctions)突然无法加载。它会设法自行恢复,但停机时间和 "dropped" blob 大约是我的 blob 的 1/5。

这看起来很愚蠢。虽然我意识到我可以通过对该异常做出反应并重新插入 blob 来缓解这种情况,但这似乎是一种资源浪费。

我的猜测是 azure 函数 运行time 有时在闲置后无法加载 dll。有没有人经历过这个并找到了解决方法?

更新: 我的函数如下所示:

    #r "MyAssembly.MyFunctions.dll"

    public static async Task Run(Stream blob, string name, TraceWriter log)
    {
        try {
            await new EventHandlerFunction().Run(name);
        } catch (Exception e) {
            log.Error(e.Message);
        }
    }

对于遇到此问题的任何人:

答案是相同程序集的多个版本位于同一功能应用程序中的不同 bin 文件夹(不同功能)中。显然,它有时会在加载程序集时找到不同的版本,这会导致提到的错误。

有两种解决方案:

  1. 最好开始使用 VS2017 工具来构建、测试和发布您的函数。这将所有程序集 在同一个bin文件夹和整个开发和发布 过程类似于 Webjobs。
  2. 或者,将所有程序集放在一个共享文件夹中,然后 引用那个而不是每个函数的特定 bin 文件夹。