azure-webjobs-sdk: Deserializing ServiceBusMessage to POCO fails

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!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

@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

@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

Does this have a milestone when to expect the fix?

cc @mathewc Updated the title to reflect the issue.

Apologies for the delayed response. The issue reproes standalone when using a servicebustrigger.

using System;
using System.Threading.Tasks;

public static void Run(ShippingCreatedMessage myQueueItem, ILogger log)
{
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}

public class ShippingCreatedMessage
{
    public Guid Id { get; set; }
    public DateTime Created { get; set; }
    public Guid OrderId { get; set; }
}

Trigger this function with payload:

// Create a new message to send to the queue

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

 var message = new Message(Encoding.UTF8.GetBytes(messageBody));

   // Write the body of the message to the console
 Console.WriteLine($"Sending message: {messageBody}");

 // Send the message to the queue
  await queueClient.SendAsync(message);

Exception is coming from here:

https://github.com/Azure/azure-webjobs-sdk/blob/6930c4ea9ec78db12a05a2d0bf38cce42ab04e13/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus/Triggers/MessageToStringConverter.cs#L46-L57

To deserialize to a POCO, Webjobs sdk needs to convert to the requested type and not string. More details here: https://abhishekrlal.com/2012/03/30/formatting-the-content-for-service-bus-messages/

I will move this issue to webjobs sdk repo. Thanks again @ChristianWeyer for detailed repro.