【问题标题】:Azure WebJobs QueueTrigger attempts (and fails) to convert message's byte[] body to stringAzure WebJobs QueueTrigger 尝试(但失败)将消息的字节 [] 正文转换为字符串
【发布时间】:2016-05-06 11:16:29
【问题描述】:

我有一个存储队列,我将使用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
  • 序列化为 JSON 的 POCO 类型
  • byte[]
  • CloudQueueMessage

那么为什么它没有绑定到我的消息?

【问题讨论】:

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


    【解决方案1】:

    WebJobs SDK 参数绑定在很大程度上依赖于神奇的参数名称。尽管[QueueTrigger(...)] string 似乎允许使用任何参数名称(并且 MSDN 文章包括示例logMessageinputTextqueueMessageblobName),但[QueueTrigger(...)] CloudQueueMessage 要求将参数命名为message。将参数名称从 msg 更改为 message 可修复绑定。

    不幸的是,我不知道有任何文档明确说明了这一点。

    【讨论】:

    • 参数的名称根本不考虑绑定逻辑 - 您可以随意命名。也许发生了其他一些变化使您的功能开始工作?不能是参数名:)
    【解决方案2】:

    试试这个:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 2017-08-24
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      相关资源
      最近更新 更多