【问题标题】:Azure Function fails to load assemblies after running fine for a whileAzure Function 在运行一段时间后无法加载程序集
【发布时间】:2017-03-22 08:44:17
【问题描述】:

我有一个函数侦听经常插入的 blob 存储,但有时很少足以让函数空闲。

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

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

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)突然无法加载。它会自行恢复,但停机时间和“丢弃”的 blob 大约是我 blob 的 1/5。

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

我的猜测是,azure 函数运行时有时会在空闲后无法加载 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);
        }
    }

【问题讨论】:

标签: c# .net azure azure-functions


【解决方案1】:

致任何偶然发现此问题的人:

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

有两种解决方案:

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

【讨论】:

  • 另外值得注意的是:这实际上意味着您不应该在同一个函数应用中拥有多个函数,除非它们始终共享相同的程序集。不可能在不更新其他函数的同时更新一个函数的程序集,因为它们是共享的。
  • 自从我最近收到了对此响应的支持...从 2020 年开始,建议的做法是不要像发布网络作业那样将单独的功能项目发布到同一个功能应用程序,而是有专门的功能应用程序用于VS中的每个项目。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
相关资源
最近更新 更多