如何管理并发 Http Triggered Azure Functions 上的数据?

How to manage data on concurrent Http Triggered Azure Functions?

我的 Azure 函数中有以下代码:

string id = Guid.NewGuid().ToString();
myObject.id = id;

现在,如果我同时进行多个 http 调用,并发调用最终会具有相同的 ID,我希望它们具有不同的 ID。

我试过更改 host.json,但没有成功。无论如何,这是 host.json:

中的代码
{
  "version": "2.0",
  "extensions": {
    "http": {
      "maxConcurrentCalls": 1
    }
  }
}

Azure 函数版本:2 网络版本:5.0

我该如何解决这个问题?

摘自 Guid.NewGuid Method 文档。

This is a convenient static method that you can call to get a new Guid. The method creates a Version 4 Universally Unique Identifier (UUID) as described in RFC 4122, Sec. 4.4. The returned Guid is guaranteed to not equal Guid.Empty.

On Windows, this function wraps a call to the CoCreateGuid function. The generated GUID contains 122 bits of strong entropy.

On non-Windows platforms, starting with .NET 6, this function calls the OS's underlying cryptographically secure pseudo-random number generator (CSPRNG) to generate 122 bits of strong entropy. In previous versions of .NET, the entropy is not guaranteed to be generated by a CSPRNG.

引用的 RFC 指出:

The version 4 UUID is meant for generating UUIDs from truly-random or pseudo-random numbers.

The algorithm is as follows:

  • Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.
  • Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the 4-bit version number from Section 4.1.3.
  • Set all the other bits to randomly (or pseudo-randomly) chosen values.

根据这些规范,让对同一个 Azure 函数的两次调用生成相同的 GUID 应该是不可能的。一些本地测试表明(在可测试的请求数量中)这是不可能的。

您可能在某处有一个静态 属性 或对象,这就是您使用相同 ID 的原因。