Azure Functions Service Trigger: getting Serialization exception when trying to bind to custom class

I'm creating an Azure Function with a Service Bus trigger and trying to bind an incoming message to my own class:

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 post is JSON like

{ "EntityId": "1234" }

      

Unfortunately binding does not work at runtime with the following message:

Function execution exception: Functions.ServiceBusTriggerCSharp1. Microsoft.Azure.WebJobs.Host: One or more errors occurred. the exception is the communication parameter "message". System.Runtime.Serialization: Waiting for element 'Submission_x0023_0.InputMessage' from namespace ' http://schemas.datacontract.org/2004/07/ ' .. Encountered 'Element' named 'string', namespace ' http: // schemas.microsoft.com/2003/10/Serialization/ '..

It looks like the runtime is trying to deserialize the message with DataContractSerializer

. How do I switch deserialization to JSON?

+3


source to share


1 answer


BrokeredMessage

that comes to a function must have a property ContentType

explicitly set to application/json

. If not specified, a default is assumed DataContractSerializer

. So, do this when posting:

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

      



See ServiceBus Serialization Scripts for details .

+6


source







All Articles