Hi everyone,
i am banging my head on the following task:
I have an AzureFunction that uses a ServiceBus-Trigger like so:
{
"bindings": [
{
"name": "myQueueItem",
"type": "serviceBusTrigger",
"direction": "in",
"connection": "MyConnection",
"accessRights": "Listen",
"topicName": "MyTopic",
"subscriptionName": "MySubscription"
}
],
"disabled": false
}
public static void Run(byte[] myQueueItem, TraceWriter log)
{
log.Info($"Incoming queue item was: {myQueueItem}");
}
On the sender side I use the following code:
private static void SendMessagesToServiceBus()
{
string senderConnectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionStringSender");
TopicClient senderClient = TopicClient.CreateFromConnectionString(senderConnectionString);
var content = Encoding.UTF8.GetBytes("Some string");
BrokeredMessage message = new BrokeredMessage(content);
message.ContentType = "text/plain";
senderClient.Send(message);
System.Console.WriteLine("Test message");
}
That works like a charm. Now th trouble starts. I have a new client that has no reference to the Aszure.ServiceBus assembly. So I can't use BrokeredMessage to send the message to service bus. Instead I tried to use the REST API of the service bus and came up with the following:
private static void SendCheckpointServiceBusApi()
{
var sas = createToken("sb://myendpoint.servicebus.windows.net/", "TopicSender", "MySecretKey");
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas);
String message = "Some string";
ByteArrayContent postContent = new ByteArrayContent(Encoding.UTF8.GetBytes(message));
postContent.Headers.Add("ContentType", "text/plain");
var response = client.PostAsync("https://myendpoint.servicebus.windows.net/topic" + "/messages" + "?timeout=60", postContent);
response.Wait();
response.Result.EnsureSuccessStatusCode();
}
Whenever sending this to the service bus I get the following exception
Exception while executing function: Functions.CheckpointToDatabase. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'myQueueItem'. System.Runtime.Serialization: There was an error deserializing the object of type System.Byte[]. The input source is not correctly formatted. System.Runtime.Serialization: The input source is not correctly formatted.
I already learned that the BrokeredMessage intenally uses Serialization. That should be switched off by adding contenttype text/plain.
Any help is most appreciated.
What I have tried:
I already tried different combinations of ContentType (text/plain, application/xml, application/json) and parameter types (string, byte[] and stream). Everyhting works when the sender site has access to BrokeredMessage. Using rest and plain UTF8 content the azure function can not deserialize.