I'm trying to read a message off the queue (notified rather than polling) using C# azure function.
I'm currently pushing a message onto the queue using a console app for now, see below.
There seems to be two different products, azure service bus and a queue within a storage account. The following code uses the later, I'm not sure how to read the message of the queue?
Many thanks,
Code example
StorageCredentials creds = new StorageCredentials(accountname, accountkey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudQueueClient queueClient = account.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("test-queue");
queue.CreateIfNotExists();
while (true)
{
CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject("my message"));
queue.AddMessage(message);
}
Update
After the comments and following links as suggested by experts trying to help I have tried the following code sample to push a message onto Azure Service Bus (i.e not a queue in a storage account)
namespace ConsoleApp1
{
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
class Program
{
const string ServiceBusConnectionString = "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx";
const string QueueName = "name_of_queue";
static IQueueClient queueClient;
public static async Task Main(string[] args)
{
const int numberOfMessages = 10;
queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
await SendMessagesAsync(numberOfMessages);
await queueClient.CloseAsync();
}
static async Task SendMessagesAsync(int numberOfMessagesToSend)
{
for (var i = 0; i < numberOfMessagesToSend; i++)
{
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the queue
try
{
await queueClient.SendAsync(message); // this line
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
When I run the code above the console window doesn't do anything, no error message just nothing..! strange.. Looking in the azure service bus overview it active message count is zero.
I'm using this sample project but the queueClient.SendAsync never returns back. Do I need to set something up in azure, permissions perhaps?
https://github.com/Azure/azure-service-bus
Error eventually received
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I can see requests within the portal service bus screen