3
votes

I'm creating an Azure Function with Service Bus trigger and trying to bind the incoming message to a custom class of mine:

public class InputMessage
{
    public string EntityId { get; set; }
}

public static string Run(InputMessage message, TraceWriter log)
{
    log.Info($"C# ServiceBus trigger function processed message: {message}");
}

My message is JSON, e.g.

{ "EntityId": "1234" }

Unfortunately, the binding fails at runtime with the following message:

Exception while executing function: Functions.ServiceBusTriggerCSharp1. Microsoft.Azure.WebJobs.Host: One or more errors occurred. Exception binding parameter 'message'. System.Runtime.Serialization: Expecting element 'Submission_x0023_0.InputMessage' from namespace 'http://schemas.datacontract.org/2004/07/'.. Encountered 'Element' with name 'string', namespace 'http://schemas.microsoft.com/2003/10/Serialization/'. .

It looks like the runtime tries to deserialize the message with DataContractSerializer. How do I switch the deserialization to JSON?

1

1 Answers

10
votes

BrokeredMessage which comes to the function must have ContentType property explicitly set to application/json. If it's not specified, the default DataContractSerializer will be assumed. So, do this when sending the message:

var message = new BrokeredMessage(body)
{
    ContentType = "application/json"
};

See ServiceBus Serialization Scenarios for details.