Azure函数应用程序-在执行和间歇性运行的旧代码之间共享全局变量

标签 azure azure-functions queuetrigger

目前,我在 Azure 函数应用程序中遇到两个问题。我提供了以下详细信息:

<强>1。全局变量内容在执行之间共享: 我使用了并发字典,它是一个全局变量,私有(private)的和静态的。该变量正在队列触发器中使用。

private static readonly ConcurrentDictionary<string, string> s_mapping = new ConcurrentDictionary<string, string>()

我间歇性地看到上述变量在不同的队列触发器执行之间共享。如何纠正它,以便变量不会在不同的运行实例之间共享?

<强>2。通过 Visual Studio 发布后运行的旧代码: 我使用 Visual Studio 发布代码,间歇性地看到旧代码正在运行。在配置中,WEBSITE_RUN_FROM_PACKAGE 设置为 1,并且我将其部署为 VS 中的 zip 文件。我尝试重新启动功能应用程序,但似乎不起作用。

非常感谢此处的帮助或有关如何解决这些问题的任何指导。

最佳答案

Each function app runs in its own process and all functions run in that process. Static variables are shared across all functions in that application just as if you'd written an ASP.NET or console app. For example, you can create an HttpClient in a static class and all functions can access that same client instance (we encourage this, in fact). It's always been this way, so nothing changed there.

来源:https://github.com/Azure/Azure-Functions/issues/1202

1-) 不要使用静态变量或拆分为不同的 Azure 函数应用。

2-) 在发布新代码之前尝试从 wwwroot 中删除文件。这种情况不应该发生,但在工作负载较高的情况下是可能的(在发布新代码时正在处理代码)。我相信您能做的最好的事情就是在推送更改之前正确设置清理步骤。

关于Azure函数应用程序-在执行和间歇性运行的旧代码之间共享全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68622162/

相关文章:

node.js - Azure AD B2C (NodeJS) : Unauthorized 401 error for a request with a valid token

c# - 我应该在 Azure WebJobsSDK 的顶层调用中使用ConfigureAwait(false)

node.js - TypeScript Azure Function 将 POST 方法的正文读取为 JSON

python - 无法在函数应用中向 Azure 队列添加多个项目

c# - 使用 Azure Active Directory 进行身份验证时进行 Azure 管理库 API 调用时出错

azure - Azure 表存储列中的高效字符串搜索

azure-functions - 在本地运行 azure Functions 时如何覆盖日志记录设置?

Azure 函数 - 如何获取应用程序见解而不记录可用性测试结果

azure - 触发Azure Webjob偶尔不运行

azure - QueueTrigger 未选取消息 - Azure WebJobs SDK 3.0