question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserializing ServiceBusMessage to POCO fails

See original GitHub issue

My setup:

  • macOS 10.13.6
  • VS Code Version 1.28.2
  • Azure Functions Core Tools (2.1.725 Commit hash: 68f448fe6a60e1cade88c2004bf6491af7e5f1df)
  • Function Runtime Version: 2.0.12134.0

Repro is here: https://github.com/ChristianWeyer/azure-functions-servicebus-serialization-repro

This is the scenario:

C# Azure Function 1: -HTTP Trigger -> creates a message and sends it into SB queue via output binding => Java Azure function: -SB queue trigger -> creates new message based on incoming message and sends it to another queue via SB queue output binding => C# Azure Function 2: -Service Bus queue trigger -> inspects received message… and does nothing 😉

The message coming from the Java function into C# Function 2 (via SB) cannot be deserialized through model binding - so this fails:

public static class NotifyClientsAboutOrderShipmentFunctions
{
    [FunctionName("NotifyClientsAboutOrderShipment")]
    public static void Run(
        [ServiceBusTrigger("shippingsinitiated", Connection = "ServiceBus")]
        ShippingCreatedMessage msg, // Issue with deserializing incoming message
        ILogger log)
    {
        log.LogInformation($"NotifyClientsAboutOrderShipment SB queue trigger function processed message: {msg}");
        
        var messageToNotify = new { orderId = msg.OrderId };

        // TODO: Notify users' clients through SignalR...
    }
}

Exception:

[02/11/2018 09:06:21] Executing 'NotifyClientsAboutOrderShipment' (Reason='New ServiceBus message detected on 'shippingsinitiated'.', Id=9b058fc4-b60b-4e50-aa1b-c10f97135767)
[02/11/2018 09:06:24] Executed 'NotifyClientsAboutOrderShipment' (Failed, Id=9b058fc4-b60b-4e50-aa1b-c10f97135767)
[02/11/2018 09:06:24] System.Private.CoreLib: Exception while executing function: NotifyClientsAboutOrderShipment. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'msg'. System.Private.DataContractSerialization: There was an error deserializing the object of type Serverless.Messages.ShippingCreatedMessage. The input source is not correctly formatted. System.Private.DataContractSerialization: The input source is not correctly formatted.
[02/11/2018 09:06:24] MessageReceiver error (Action=UserCallback, ClientId=MessageReceiver1shippingsinitiated, EntityPath=shippingsinitiated, Endpoint=cw-serverless-microservices.servicebus.windows.net)
[02/11/2018 09:06:24] System.Private.CoreLib: Exception while executing function: NotifyClientsAboutOrderShipment. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'msg'. System.Private.DataContractSerialization: There was an error deserializing the object of type Serverless.Messages.ShippingCreatedMessage. The input source is not correctly formatted. System.Private.DataContractSerialization: The input source is not correctly formatted.

Not too helpful with the missing details 😉

If I change the function to look like this (and do the deserialization manually), it works:

public static class NotifyClientsAboutOrderShipmentFunction
{
    [FunctionName("NotifyClientsAboutOrderShipment")]
    public static void Run(
        [ServiceBusTrigger("shippingsinitiated", Connection = "ServiceBus")]
        /*ShippingCreatedMessage*/ string msg, // Issue with deserializing incoming message
        ILogger log)
    {
        log.LogInformation($"NotifyClientsAboutOrderShipment SB queue trigger function processed message: {msg}");

        ShippingCreatedMessage message = JsonConvert.DeserializeObject<ShippingCreatedMessage>(msg);
        
        var messageToNotify = new { orderId = message.OrderId };

        // TODO: Notify users' clients through SignalR...
    }
}

This is the message being sent via the ‘response’ queue:

{
  "Id": "c497b524-5929-48e5-aa7e-1a5992678afb",
  "Created": "2018-11-02T09:15:20.000133Z",
  "OrderId": "9190338c-546d-4d44-a7f3-081d6563bfc8"
}

What is expected:

  1. Automatic deserialization should properly work
  2. If it does not work, a more detailed & more helpful exception message should be shown why it did not work

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
SeanFeldmancommented, May 21, 2019

@ChristianWeyer deserialization to POCO does work if

  1. You specify ContentType header with application/json as @alrod pointed out
  2. Send the message using the new ASB client
1reaction
alrodcommented, May 21, 2019

@SeanFeldman, we are expecting POCO object desirialized as xml by default. If you want to send JSON please add “application/json” ContentType to your message.

https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus/Triggers/UserTypeArgumentBindingProvider.cs#L74

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure ServiceBus Message Serialization/Deserialization
I am using a .NET Core application to send an object through an Azure Service Bus Queue and have it received by a...
Read more >
Azure ServiceBus Queue - Serialization operation failed ...
Hi, I'm trying to send custom message to Azure ServiceBuz Queue. The custom message has a property which accepts Byte[].
Read more >
Azure Service Bus message transfers, locks, and settlement
This article provides an overview of Azure Service Bus message transfers, locks, and settlement operations.
Read more >
Azure Functions Isolated Worker - Sending multiple messages
To output multiple items to the same output binding, we need to resort to a bit of tedious work. First, we'll need to...
Read more >
Logic Apps fails to parse ServiceBus messages from Azure ...
After some investigation, we realized that the ServiceBus message wasn't actually a JSON message. It was preceded by string serialization ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found