Servicebustrigger反序列化失败 [英] Servicebustrigger deserialization fails

查看:84
本文介绍了Servicebustrigger反序列化失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用servicebus的简单网络作业:

 公共异步任务ProcessQueueMessage([ServiceBusTrigger("asset-updates-in")] BrokeredMessage消息,ILogger日志){log.LogInformation(正在运行的作业");log.LogInformation("GotMessage" + message.ContentType); 

消息是由Biztalk团队发送的,具有MIME类型"text/xml"看起来像这样:

 < gip:GetDeviceUpdateResult xmlns:gip ="http://schemas.ores.net/customer/breakdown-manager/1.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance>< gip:Event>< gip:Id> 5886</gip:Id>< gip:Action> Upsert</gip:Action>< gip:Timestamp> 2019-09-20T13:35:40</gip:Timestamp></gip:Event>< gip:Cabin>< gip:Id> 5001874</gip:Id>< gip:BusinessId> 029843</gip:BusinessId>< gip:Name> RUE DE LANDEN TEST 2</gip:Name>< gip:DistrictId> 1590</gip:DistrictId></gip:Cabin></gip:GetDeviceUpdateResult> 

我的网络工作收到消息后,我便出现此错误:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException:异常执行函数时:UpdateFunctions.ProcessQueueMessage --->System.InvalidOperationException:异常绑定参数'消息'---> System.InvalidOperationException:将参数绑定到复杂对象(例如"BrokeredMessage")使用Json.NET序列化或XML对象序列化.

  1. 如果ContentType为'application/json',则反序列化为JSON
  2. 如果ContentType不是'application/json',则尝试使用Message.GetBody进行反序列化,这将处理XML对象之类的情况序列化
  3. 如果该反序列化失败,请最后尝试进行JSON反序列化,以发现可能是内容类型为不正确

JSON解析器失败:解析时遇到意外字符价值: ?.路径",第0行,位置0.

因此,当数据是来自atat的xml时,似乎要使用json解析器.

如果我这样更改我的网络作业:

 公共异步任务ProcessQueueMessage([ServiceBusTrigger("asset-updates-in")]字符串消息,ILogger日志){log.LogInformation(正在运行的作业");log.LogInformation("GotMessage" +消息); 

我得到了:

  [09/20/2019 11:38:43>da4c4f:INFO] GotMessage<?xml版本="1.0"编码="utf-8"?>< gip:GetDeviceUpdateResult xmlns:gip ="http://schemas.ores.net/customer/breakdown-manager/1.0"xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance">< gip:Event>< gip:Id> 5889</gip:Id>< gip:Action> Upsert<;/gip:Action>< gip:Timestamp> 2019-09-20T13:38:11</gip:Timestamp></gip:Event>< gip:Cabin>< gip:Id> 5001874</gip:Id>< gip:BusinessId> 029843</gip:BusinessId>< gip:Name> RUE DE LANDEN</gip:Name>< gip:DistrictId> 1590</gip:DistrictId></gip:Cabin>;</gip:GetDeviceUpdateResult>[09/20/2019 11:38:43>da4c4f:INFO]信息:Function.ProcessQueueMessage [0] 

我想我可以反序列化自己,但是有点恶臭……我缺少了什么?

谢谢!

解决方案

这是因为在Webjob 3.x中不推荐使用 BrokeredMessage 类.WebJobs SDK版本3.x正在使用新的.NET Standard服务总线客户端( Microsoft.Azure.ServiceBus

I have a simple webjob that use servicebus:

   public async Task ProcessQueueMessage([ServiceBusTrigger("asset-updates-in")] BrokeredMessage message,
        ILogger log)
    {
        log.LogInformation("Running job");
        log.LogInformation("GotMessage"+ message.ContentType);

Message is sent by Biztalk team, has a mime type 'text/xml' And looks like this:

 <gip:GetDeviceUpdateResult xmlns:gip="http://schemas.ores.net/customer/breakdown-manager/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <gip:Event>
        <gip:Id>5886</gip:Id>
        <gip:Action>Upsert</gip:Action>
        <gip:Timestamp>2019-09-20T13:35:40</gip:Timestamp>
      </gip:Event>
      <gip:Cabin>
        <gip:Id>5001874</gip:Id>
        <gip:BusinessId>029843</gip:BusinessId>
        <gip:Name>RUE DE LANDEN TEST 2</gip:Name>
        <gip:DistrictId>1590</gip:DistrictId>
      </gip:Cabin>
    </gip:GetDeviceUpdateResult>

As soon as my webjob gets the message i have this error:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: UpdateFunctions.ProcessQueueMessage ---> System.InvalidOperationException: Exception binding parameter 'message' ---> System.InvalidOperationException: Binding parameters to complex objects (such as 'BrokeredMessage') uses Json.NET serialization or XML object serialization.

  1. If ContentType is 'application/json' deserialize as JSON
  2. If ContentType is not 'application/json' attempt to deserialize using Message.GetBody, which will handle cases like XML object serialization
  3. If this deserialization fails, do a final attempt at JSON deserialization to catch cases where the content type might be incorrect

The JSON parser failed: Unexpected character encountered while parsing value: ?. Path '', line 0, position 0.

So it seems to using json parser while data is xml fromat...

If I change my webjob like this:

 public async Task ProcessQueueMessage([ServiceBusTrigger("asset-updates-in")] string message,
        ILogger log)
    {
        log.LogInformation("Running job");
        log.LogInformation("GotMessage"+ message);

I get :

[09/20/2019 11:38:43 > da4c4f: INFO]       GotMessage<?xml version="1.0" encoding="utf-8"?><gip:GetDeviceUpdateResult xmlns:gip="http://schemas.ores.net/customer/breakdown-manager/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><gip:Event><gip:Id>5889</gip:Id><gip:Action>Upsert</gip:Action><gip:Timestamp>2019-09-20T13:38:11</gip:Timestamp></gip:Event><gip:Cabin><gip:Id>5001874</gip:Id><gip:BusinessId>029843</gip:BusinessId><gip:Name>RUE DE LANDEN</gip:Name><gip:DistrictId>1590</gip:DistrictId></gip:Cabin></gip:GetDeviceUpdateResult>
[09/20/2019 11:38:43 > da4c4f: INFO] info: Function.ProcessQueueMessage[0]

I guess I could deserialize myself, but it stinks a bit... I am missing something?

Thanks!

解决方案

This is because the BrokeredMessage class is deprecated in the webjob 3.x. WebJobs SDK version 3.x is using the new .NET Standard service bus client (Microsoft.Azure.ServiceBus with Message class).

So you could use this to deserialize it.

public static void processservicebus(
        [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")]Message message,
        ILogger log)
        {

            log.LogInformation(message.ContentType);

            XDocument orderOut = XDocument.Parse(Encoding.UTF8.GetString(message.Body));

        }

这篇关于Servicebustrigger反序列化失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆