Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordan112/9ae4346f01658dea7f6d63e22552425b to your computer and use it in GitHub Desktop.
Save jordan112/9ae4346f01658dea7f6d63e22552425b to your computer and use it in GitHub Desktop.
using Microsoft.Azure.ServiceBus;
using Microsoft.ServiceBus;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Amway.ServiceBus.MessageSendTester
{
class Program
{
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
//const string ServiceBusConnectionString = "Endpoint=sb://amway-warrantyservice-queue-dev.servicebus.windows.net/;SharedAccessKeyName=LocalMarketDispatchOutput;SharedAccessKey=rHst5BBr1akdQUu5Qp8vhA84FPX+uM6gqn2uSJ20r1w=;";
//const string TopicName = "localmarketfeed";
const string ServiceBusConnectionString = "Endpoint=sb://amwayshanetest.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Jw3t5SEdnoMmt/rM5m8TmW5OTONGbKJWTfj3uZjBcdg=";
const string TopicName = "shanetest";
const string QueueName = "shanetest";
static ITopicClient topicClient;
static IQueueClient queueClient;
static async Task MainAsync()
{
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https;
const int numberOfMessages = 10;
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
// queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
Console.WriteLine("======================================================");
Console.WriteLine("Press ENTER key to exit after sending all the messages.");
Console.WriteLine("======================================================");
// Send messages.
await SendMessagesAsync(numberOfMessages);
//await SendMessagesQueueAsync(numberOfMessages);
Console.ReadKey();
await topicClient.CloseAsync();
}
static async Task SendMessagesQueueAsync(int numberOfMessagesToSend)
{
try
{
for (var i = 0; i < numberOfMessagesToSend; i++)
{
// Create a new message to send to the topic
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the topic
await queueClient.SendAsync(message);
}
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
static async Task SendMessagesAsync(int numberOfMessagesToSend)
{
try
{
for (var i = 0; i < numberOfMessagesToSend; i++)
{
// Create a new message to send to the topic
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the topic
await topicClient.SendAsync(message);
}
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment