Click here to Skip to main content
15,936,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

i am banging my head on the following task:

I have an AzureFunction that uses a ServiceBus-Trigger like so:

Json#
{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "connection": "MyConnection",
      "accessRights": "Listen",
      "topicName": "MyTopic",
      "subscriptionName": "MySubscription"
    }
  ],
  "disabled": false
}


C#
public static void Run(byte[] myQueueItem, TraceWriter log)
{
    log.Info($"Incoming queue item was: {myQueueItem}");
}


On the sender side I use the following code:

C#
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:

C#
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

C#
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.
Posted
Updated 16-Jun-17 4:28am

1 solution

Your problem clearly lies in the difference between these lines:
var content = Encoding.UTF8.GetBytes("Some string");

ByteArrayContent postContent = new ByteArrayContent(Encoding.UTF8.GetBytes(message));

Why use the ByteArrayContent?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900