0

I have a simple message sender in C#

public static async Task SendMessageToTopic(string serviceBusConnectionString, string topic, string message)
{
    const int numberOfMessages = 10;
    topicClient = new TopicClient(serviceBusConnectionString, topic);

    Console.WriteLine("======================================================");
    Console.WriteLine("Press ENTER key to exit after sending all the messages.");
    Console.WriteLine("======================================================");

    // Send messages.
    await SendMessagesAsync(message);

}

static async Task SendMessagesAsync(string message)
{
    try
    {
            var encodedMessage = new Message(Encoding.UTF8.GetBytes(message));

            // Write the body of the message to the console.
            Console.WriteLine($"Sending message: {encodedMessage}");

            // Send the message to the topic.
            await topicClient.SendAsync(encodedMessage);

    }
    catch (Exception exception)
    {
        Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
    }
}

In the other end I have a simple receiver application and everything works well. Now I want to store my messages (which are, say, integers) into Azure storage to plot them later. I set up endpoints and routes as described here https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-store-data-in-azure-table-storage#verify-your-message-in-your-storage-container But it looks like my container is empty (I use Azure Storage Explorer to see it)

amplifier
  • 1,793
  • 1
  • 21
  • 55
  • Show us the code on how you're storing the data... This is only the send-part, which you said works fine. – rickvdbosch Jun 13 '18 at 14:00
  • the thing is that I thought that setting up routing automatically would allow to store messages. I don't have any code for storing – amplifier Jun 13 '18 at 14:06

1 Answers1

1

I think there are something need to be clarified. Service Bus can be used as one of the additional endpoints for Azure IoT Hub. IoT Hub currently supports Azure Storage containers,Event Hubs,Service Bus Queues and Service Bus Topics as additional endpoints.IoT Hub needs write access to or configure(via Azure Portal)these service endpoints for message routing to work. More info here about how to understand IoT Hub Endpoints and configure message routing with Azure IoT Hub. The endpoint you have set up is for the IoT hub not Service Bus.

Service Bus itself does not support routing the received message to storage container or other endpoints. If you want to store the received message of Service Bus, you can use Azure Service Bus bindings for Azure Functions.Azure Functions supports trigger and output bindings for Service Bus queues and topics. This blog shows how to route the queue in run time with Azure Functions bindings dynamically.

C# for Service Bus Topic Trigger Example(run.csx):

#r "Microsoft.WindowsAzure.Storage"

using System;
using System.Configuration;
using System.Net;
using System.Text;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task Run(string mySbMsg, TraceWriter log)
{
    log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

    string connectionString="{storage acount connectionString}";
    CloudStorageAccount storageAccount;
    CloudBlobClient client;
    CloudBlobContainer container;
    CloudBlockBlob blob;

    storageAccount = CloudStorageAccount.Parse(connectionString);

    client = storageAccount.CreateCloudBlobClient();

    container = client.GetContainerReference("container01");

    await container.CreateIfNotExistsAsync();

    blob = container.GetBlockBlobReference(DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".json");
    blob.Properties.ContentType = "application/json";

    using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(mySbMsg)))
    {
        await blob.UploadFromStreamAsync(stream);
    }
}
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Awesome, it works! Btw, I opened the last link you mentioned and there ServiceBusTrigger attribute is used, but as I said, your code works without attributes. Could you clarify please, whether I should use the attributes in any cases? – amplifier Jun 14 '18 at 13:58
  • 1
    The attribute is used to bind a parameter to a ServiceBus Queue message, causing the function to run when a message is enqueued. It handles all of the bookkeeping required to monitor a Service Bus queue. – Michael Xu Jun 15 '18 at 01:24
  • Could you please answer another my question https://stackoverflow.com/questions/51104579/azure-topic-and-sending-message-over-mqtt – amplifier Jun 30 '18 at 12:56