Deserializing ServiceBusMessage to POCO fails
See original GitHub issueMy 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:
- Automatic deserialization should properly work
- If it does not work, a more detailed & more helpful exception message should be shown why it did not work
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:21 (10 by maintainers)
Top GitHub Comments
@ChristianWeyer deserialization to POCO does work if
application/json
as @alrod pointed out@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