gpt4 book ai didi

c# - 如何从 Azure 存储队列中的消息文本字段读取 JSON 字符串(使用 Azure 函数)?

转载 作者:行者123 更新时间:2023-12-03 01:20:29 25 4
gpt4 key购买 nike

我尝试将 Azure 函数绑定(bind)到队列并收到以下错误:

enter image description here

The input is not a valid Base-64 string as it contains a non-base 64character, more than two padding characters or an illegal characteramong the padding characters

这是队列的示例:

enter image description here

以下是用于尝试读取队列的代码:

代码:

namespace GetInvoicePayload
{
public static class LoadtoDB
{
[FunctionName("LoadtoDB")]
public static void Run([QueueTrigger("incoming", Connection = "ConnectionStrings")] string myQueueItem, ILogger log)
{

//string json = JsonConvert.SerializeObject(myQueueItem);

//byte[] json = Encoding.ASCII.GetBytes(myQueueItem);
File.WriteAllText("C:\\dspl\\WriteLines.txt", myQueueItem);


log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
}
}

JSON 字符串通过以下代码片段进行序列化(序列化 ExpandoObject)

string queueMessage = JsonConvert.SerializeObject(queueMessageE);

QueueClient queueClient = new QueueClient(connectionString, "incoming");

if (queueClient.Exists())
{
// Send a message to the queues
queueClient.SendMessage(queueMessage);
}

编辑:我根据下面的评论添加了以下内容,但仍然收到相同的错误消息。我已经检查过,队列数据是正确的。

enter image description here

这是 postman 有效负载:

enter image description here

以及队列接收到的内容(由于错误,现在位于有害队列中):

enter image description here

最佳答案

您收到此错误的原因是队列触发的 Azure Functions 期望消息正文采用 bas64 编码。

从此link :

Functions expect a base64 encoded string. Any adjustments to theencoding type (in order to prepare data as a base64 encoded string)need to be implemented in the calling service.

您需要做的是将 bas64 编码的消息发送到队列,然后在函数代码中将其解码。

类似于:

queueClient.SendMessage(Convert.ToBase64String(Encoding.UTF8.GetBytes(queueMessage)));

string messageBody = Encoding.UTF8.GetString(Convert.FromBase64String(myQueueItem));

关于c# - 如何从 Azure 存储队列中的消息文本字段读取 JSON 字符串(使用 Azure 函数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70812691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com