Azure WebJobs QueueTrigger尝试(并且失败)将消息的byte []正文转换为字符串

时间:2016-05-06 11:16:29

标签: binding parameter-passing azure-webjobs azure-webjobssdk

我有一个存储队列,我发布使用CloudQueueMessage(byte[])构造函数构造的消息。然后我尝试使用以下签名处理webjob函数中的消息:

public static void ConsolidateDomainAuditItem([QueueTrigger("foo")] CloudQueueMessage msg)

我遇到异常

的失败
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Program.ConsolidateDomainAuditItem ---> System.InvalidOperationException: Exception binding parameter 'msg' ---> System.Text.DecoderFallbackException: Unable to translate bytes [FF] at index -1 from specified code page to Unicode.
 at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
 at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index)
 at System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte* pBytes)
 at System.Text.UTF8Encoding.GetCharCount(Byte* bytes, Int32 count, DecoderNLS baseDecoder)
 at System.String.CreateStringFromEncoding(Byte* bytes, Int32 byteLength, Encoding encoding)
 at System.Text.UTF8Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
 at Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage.get_AsString()
 at Microsoft.Azure.WebJobs.Host.Storage.Queue.StorageQueueMessage.get_AsString()
 at Microsoft.Azure.WebJobs.Host.Queues.Triggers.UserTypeArgumentBindingProvider.UserTypeArgumentBinding.BindAsync(IStorageQueueMessage value, ValueBindingContext context)
 at Microsoft.Azure.WebJobs.Host.Queues.Triggers.QueueTriggerBinding.<BindAsync>d__0.MoveNext()

查看UserTypeArgumentBindingProvider.BindAsync的代码,显然希望传递一个主体是JSON对象的消息。名称的UserType...也暗示它希望绑定POCO。

然而,MSDN文章How to use Azure queue storage with the WebJobs SDK明确指出

  

您可以将QueueTrigger与以下类型一起使用:

     
      
  • string
  •   
  • POCO类型序列化为JSON
  •   
  • byte[]
  •   
  • CloudQueueMessage
  •   

那么为什么它不能绑定我的信息?

2 个答案:

答案 0 :(得分:0)

WebJobs SDK参数绑定在很大程度上依赖于魔术参数名称。虽然[QueueTrigger(...)] string似乎允许任何参数名称(并且MSDN文章包含示例logMessageinputTextqueueMessageblobName),[QueueTrigger(...)] CloudQueueMessage要求参数名为message。将参数名称从msg更改为message可修复绑定。

不幸的是,我并不知道任何明确说明这一点的文件。

答案 1 :(得分:0)

请改为尝试:

public static void ConsolidateDomainAuditItem([QueueTrigger(“foo”)] byte [] message)

CloudQueueMessage是一个包装器,通常绑定摆脱了包装器,允许您处理内容。