public async Task UpdateStockAsync(ProductInStockUpdateStockCommand command) { var queueClient = new Microsoft.Azure.ServiceBus.QueueClient(_connectionString, "order-stock-update"); // Serialize message string body = JsonSerializer.Serialize(command); var message = new Message(Encoding.UTF8.GetBytes(body)); // Send the message to the queue await queueClient.SendAsync(message); // Close await queueClient.CloseAsync(); }
public static async Task GenerateForecastServiceBusActivity([ActivityTrigger] string details) { // Deserialize the forecast details so that we can determine which partion // key and queue to work with. var messageDetails = JsonConvert.DeserializeObject <ForecastMessageDetails>(details); // Get a reference to the table that will contain all the coordinates. var storageAccount = CloudStorageAccount.Parse(StorageConnectionString); var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference("datapoints"); await table.CreateIfNotExistsAsync(); // Get a reference to the service bus queue that we will send the messages to. var queueClient = new Microsoft.Azure.ServiceBus.QueueClient(ServiceBusConnectionString, messageDetails.QueueName); // Get the coordinates from the table and add each one as a separate message // to the service bus queue. var coordinates = await GetCoordinatesFromStorage(messageDetails.PartitionKey, table); var messagesToSend = new List <Message>(); foreach (var c in coordinates) { var forecastRequest = new List <Coordinates> { new Coordinates { Latitude = c.Latitude, Longitude = c.Longitude } }; var serializedMessage = JsonConvert.SerializeObject(forecastRequest); var message = new Message(Encoding.UTF8.GetBytes(serializedMessage)); messagesToSend.Add(message); } // Service bus allows you to add messages in a batch. await queueClient.SendAsync(messagesToSend); }
static void Main(string[] args) { string sbConnectionString = "Endpoint=sb://miniproject.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=tGkzmzkJl5PqEBSAQxiWA92Dt0wV9HSBOh7QS6k4OGo="; string sbQueName = "recharge"; string messageBody = string.Empty; do { try { Console.WriteLine("----------------------------------------------------"); Console.WriteLine("Mobile Recharge information"); Console.WriteLine("----------------------------------------------------"); Console.WriteLine("Mobile operator list"); Console.WriteLine("1. Greemen phone"); Console.WriteLine("2. Aktel"); Console.WriteLine("3. Banglalink"); Console.WriteLine("4. Citycell"); Console.WriteLine("----------------------------------------------------"); Console.WriteLine("Choose the operator to recharge:"); string mobileOperator = Console.ReadLine(); Console.WriteLine("Recharge amount:"); string amount = Console.ReadLine(); Console.WriteLine("Enter mobile number"); string moileNo = Console.ReadLine(); Console.WriteLine("----------------------------------------------------"); switch (mobileOperator) { case "1": mobileOperator = "Greemen phone"; break; case "2": mobileOperator = "Aktel"; break; case "3": mobileOperator = "Banglalink"; break; case "4": mobileOperator = "Citycell"; break; default: break; } messageBody = "Operator Name:" + mobileOperator + "\n Phone No:" + moileNo + "\n Amount:" + amount; queueClient = new Microsoft.Azure.ServiceBus.QueueClient(sbConnectionString, sbQueName); var message = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(messageBody)); Console.WriteLine($"Message Added in Queue: {messageBody}"); queueClient.SendAsync(message); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.ReadKey(); queueClient.CloseAsync(); } Console.WriteLine("Do you want to send more message: y/n"); s = Console.ReadLine().ToString(); Console.ReadKey(); }while (s == "y"); }