c# - 跨 Azure 进程锁定多个函数

标签 c# azure azure-webjobs

我有两个使用不同 Web 作业名称(分别)运行的 Web 作业,它们有时会尝试运行相同的函数(进而修改数据)。我想确保它们不能同时运行。由于它们在两个不同的进程中运行,因此我无法使用 C# lock() 机制。我也无法使用 [Singleton] 属性,因为它们是具有不同名称的单独 Web 作业。

您建议如何处理这个问题?我正在考虑诸如 Lease Blob 操作(到存储帐户)之类的操作,但无法获得有关执行此操作的太多信息。

或者我猜是某种阻塞事务?

最佳答案

看来您可以在 Web 作业函数上使用 [Singleton] 属性来实现多种目的,包括实现更精细和更粗糙的应用程序。

来自Sdk Documentation ,您可以使用 SingletonScope.Host 选项在不同函数之间共享锁,前提是给定名称相同:

Locking Across Different Functions

In addition to using Singleton locks scoped to a single function, you can also declare locks that work across multiple functions. In this example we show two functions that can be triggered independently, but we want to ensure that only one of them runs at any given time:

[Singleton("ItemsLock", SingletonScope.Host)]
public static void AddItem([QueueTrigger("add-item")] string message)
{
     // Perform the add operation
}

[Singleton("ItemsLock", SingletonScope.Host)]
public static void RemoveItem([QueueTrigger("remove-item")] string message)
{
     // Perform the remove operation
}

关于c# - 跨 Azure 进程锁定多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47524130/

相关文章:

c# - 在 C# 中处理非常大的 Dictionary<>

c# - 在 Unity 5 中从本地服务器创建和下载 Assets 包

c# - MVVM 和 View/ViewModel 层次结构

c++ - Azure C++ 库 : "Invalid streambuf object"

azure - 按 EmployeeID 更新多个用户 ADGroup

azure-webjobs - Azure Web 作业体系结构

c# - 在 C# .NET Framework 中使用 ConfigurationBuilder 我的应用服务设置去了哪里?

c# - 使用 StreamWriter Async 时出现错误 "The stream is currently in use by a previous operation on the stream"

azure - 如何通过私有(private)链接从Azure数据工厂连接Azure批处理?

azure-web-app-service - 将代码部署到Azure多实例Web应用程序