1

我想将 int 值保存到队列消息中,然后在 Azure Function QueueTrigger 上获取它。

我通过以下方式保存:

        int deviceId = -1;
        await queue.AddMessageAsync(new CloudQueueMessage(deviceId.ToString()));

然后听队列:

    public async Task Run(
        [QueueTrigger("verizon-suspend-device", Connection = "StorageConnectionString")] string queueMessage, 
        ILogger log)
    {
        int deviceId = int.Parse(queueMessage);

但所有消息都被移到verizon-suspend-device-poison队列中。怎么了?

4

1 回答 1

3

不幸的是,Azure 函数队列触发器目前需要 Base64 编码的消息。如果您针对存储模拟器运行代码,您应该会从触发器中看到一个异常,例如The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

现在,至少(即,直到触发器可以处理二进制/utf8 消息),排队代码必须将消息作为 Base64 字符串放在队列中。消息的队列触发代码在此处string结束,并采用 Base64 编码AsString

对于存储 SDK 的那个(旧)版本,您可以发送整数的 Base64 编码的 UTF-8 字符串表示:

var bytes = Encoding.UTF8.GetBytes(deviceId.ToString());
await queue.AddMessageAsync(new CloudQueueMessage(Convert.ToBase64String(bytes), isBase64Encoded: true));

使用新的存储 SDK 您可以设置toMessageEncodingQueueClientQueueMessageEncoding.Base64,然后只发送整数的字符串表示形式(新的存储 SDK 将为您进行 UTF-8 编码,然后是 Base64 编码)。

于 2021-02-21T02:40:08.127 回答