Servicebustrigger deserialization fails

丶灬走出姿态 提交于 2021-01-07 02:41:50

问题


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!


回答1:


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));

        }



来源:https://stackoverflow.com/questions/58028180/servicebustrigger-deserialization-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!