How to send a binary message from Azure Function to Azure Service Bus

887 Views Asked by At

I used the following code to send some sample binary messages to a service bus from an Azure function, but on the service bus I always receive a message with the content "System.Byte[]" and the content type "text/plain". I also tried to read the messages from the service bus with a Java app and I get the same result. When reading the messages with the Java app I expect to receive the specified array new byte[] { 1, 3, 4, 5, 6, 7, 8, 9}, not the byte array that represents the String "System.Byte[]". I assume that the azure function sends the binary array as a String instead of byte array.

package azure.functions;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.TimerTrigger;
import com.microsoft.azure.functions.annotation.ServiceBusQueueOutput;

public class ServiceBusPeriodicSampleProducer {
    @FunctionName("ServiceBusPeriodicSampleProducer")
    @ServiceBusQueueOutput(
        name = "serviceBusBinaryMessage", 
        queueName = "test-queue", 
        connection = "ServiceBusConnection", 
        dataType = "binary"
    )
    public byte[] sendSampleMessageToServiceBus(
        @TimerTrigger(name = "timerInfo", schedule = "0 * * * * *") String timerInfo, final ExecutionContext context
    ) {
        context.getLogger().info("Java Timer trigger function executed at: " + java.time.LocalDateTime.now());
        return new byte[] { 1, 3, 4, 5, 6, 7, 8, 9};
    }
}

1

There are 1 best solutions below

1
On

Based on the docs, it looks like you need to add a parameter of type OutputBinding<Byte[]>, and set the value in that parameter using outputBinding.SetValue(bytes), as opposed to having the bytes be returned from the method.