'Receive raw data from Service Bus Trigger

I am trying to build an Azure Function to process Dynamics 365 messages from an Azure Service Bus. The message is serialized in the .NET Binary XML format, so I am trying to receive the message as binary and deserialize it in my code.

The signature of my function method is:

public static void Run(
    [ServiceBusTrigger("queue", Connection = "...")] byte[] message,
    ILogger log,
    MessageReceiver messageReceiver)

However the functions runtime still fails while trying to parse message, as I get following error:

The Message with ContentType 'application/msbin1' failed to deserialize to a string with the message: 'Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'RemoteExecutionContext', namespace 'http://schemas.microsoft.com/xrm/2011/Contracts'.'. System.Private.DataContractSerialization: Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'RemoteExecutionContext', namespace 'http://schemas.microsoft.com/xrm/2011/Contracts'.

How can I receive raw data using ServiceBusTrigger?

EDIT 1: The same error occurs with

public static void Run(
    [ServiceBusTrigger("queue", Connection = "...")] Message message,
    ILogger log)

I use following versions Microsoft.Azure.WebJobs.Extensions.ServiceBus 4.1.0 and Microsoft.NET.Sdk.Functions 3.0.7.

EDIT 2:
Updated versions to Microsoft.Azure.WebJobs.Extensions.ServiceBus 4.1.1 and Microsoft.NET.Sdk.Functions 3.0.11 the error is gone, but message length is 0 bytes for both byte[] and Message.Content

EDIT 3:

Azure Functions Core Tools
Core Tools Version:       3.0.3160 Commit hash: 00aa7f43cc5c5f15241b5e6e5363256f19ceb990
Function Runtime Version: 3.0.14916.0


Solution 1:[1]

ContentType needs to be application/json. See this GitHub issue for the details.

Solution 2:[2]

Make sure your message format is JSON and NOT .NETBinary

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sean Feldman
Solution 2 hannes neukermans