Azure Functions:队列触发器需要 Base-64 消息,但未正确处理它们

Azure Functions: Queue Trigger is expecting Base-64 messages and doesn#39;t process them correctly(Azure Functions:队列触发器需要 Base-64 消息,但未正确处理它们)
本文介绍了Azure Functions:队列触发器需要 Base-64 消息,但未正确处理它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个队列触发器.预期是当我在 Queue 中插入消息时,触发器必须触发并处理出队的消息.

 [FunctionName("NewPayrollQueueTrigger")]公共异步静态无效运行([QueueTrigger(myqueue",连接="AzureWebJobsStorage")]string myQueueItem,[DurableClient] IDurableOrchestrationClient 启动器,ILogger 日志){log.LogInformation($"C# 队列触发函数处理:{myQueueItem}");await starter.StartNewAsync("NewPayrollOrchestrator", input: myQueueItem);}

触发器正在正常激活,但正在发生这种奇怪的行为.该函数显然期望 messageBase-64 编码.

<块引用>

异常绑定参数'myQueueItem' <--- 输入不是有效的 Base-64 字符串,因为它包含一个非 base 64 字符,多于两个填充字符,或填充中的非法字符字符.

我正在使用来自 Azure.Storage.QueuesAzure Queue 库 v.12 中的此方法向队列发送消息,但没有发现对消息进行编码的重载Base-64.请注意,_queue

实现此功能的唯一方法是,如果我使用 Azure 存储资源管理器选择通过 UI 对消息进行编码,手动将消息发送到队列.

解决方案

作为替代方案,您可以通过使用 QueueClientOptions 创建客户端,利用内置功能将消息转换为 Base64MessageEncoding 设置为 QueueMessageEncoding.Base64.

例如:

_queue = new QueueClient(connectionString, queueName, new QueueClientOptions{MessageEncoding = QueueMessageEncoding.Base64});...var message = "一些消息";等待_queue.SendMessageAsync(消息);//将转换为 Base64.

I have this Queue Trigger. The expected is when I insert a message in the Queue, the trigger must fire and process the dequeued message.

    [FunctionName("NewPayrollQueueTrigger")]
    public async static void Run([QueueTrigger("myqueue", Connection = 
    "AzureWebJobsStorage")]string myQueueItem,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        await starter.StartNewAsync("NewPayrollOrchestrator", input: myQueueItem);

    }

The trigger is being activated normally, but this weird behavior is happening. The function apparently expects that the message is encoded in Base-64.

Exception binding parameter 'myQueueItem' <--- 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.

I'm sending messages to the queue using this method from the Azure Queue library v.12 from Azure.Storage.Queues and found no overloads that encodes the message to Base-64. Note that _queue is a QueueClient instance.

 public async Task<Response<SendReceipt>> SendAsync(string message)
 {
        return await _queue.SendMessageAsync(message);
 }

So I tried to encode the message by myself...

 public async Task<Response<SendReceipt>> SendAsBase64Async(string message)
 {
      byte[] buffer = Encoding.Unicode.GetBytes(message);
      string msg = Convert.ToBase64String(buffer);

      return await _queue.SendMessageAsync(msg);

 }

... and it doesn't work either. Here's my code passing by that part but throwing error further on, indicating that it could get the message but it was not decoded correctly, since it was a filename of an existing blob in a storage:

The only way to get this working is if I manually send a message to the queue using the Azure Storage Explorerchoosing for encode the message via UI.

解决方案

As an alternative, you can take advantage of the built in functionality for converting the message to Base64 by creating the client with an QueueClientOptions with MessageEncoding set to QueueMessageEncoding.Base64.

For example:

_queue = new QueueClient(connectionString, queueName, new QueueClientOptions
{
    MessageEncoding = QueueMessageEncoding.Base64
});

...

var message = "some message";
await _queue.SendMessageAsync(message); // Will be converted as Base64.

这篇关于Azure Functions:队列触发器需要 Base-64 消息,但未正确处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)