Azure Service Bus Explained | Why It Exists + Simple C# Demo
Автор: Luke Saunders (System Integration)
Загружено: 2025-10-19
Просмотров: 120
Описание:
In this video, we dive into Azure Service Bus — Microsoft’s enterprise-grade messaging service.
🔹 We’ll start by covering why Azure Service Bus exists and where it fits into modern cloud architecture.
🔹 Next, we’ll break down how it works, including queues, topics, and message handling.
🔹 Finally, we’ll build a simple C# application step by step to send and receive messages using Azure Service Bus.
Whether you’re new to messaging systems or want a quick refresher with a practical example, this tutorial will give you a solid foundation to get started.
📌 What you’ll learn:
🔹Why Service Bus is used in distributed systems
🔹Core concepts (queues, topics, subscriptions)
🔹How to implement a simple sender and receiver in C#
=== Timestamps ===
0:00 - Intro
0:41 - What does Azure Service Bus offer?
6:31 - Setting up an Azure Service Bus
13:41 - Building our C# App and testing the Queue
☕ SUPPORT THE CHANNEL
If you’re enjoying these videos and want to help me make more, consider buying me a coffee:
https://buymeacoffee.com/lukesaunders
#azureservicebus
#azuretutorials
C# code:
Receiver.cs
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
class Receiver
{
private const string connectionString = "Connection string here";
private const string queueName = "queue name here";
static async Task Main(string[] args)
{
Console.WriteLine($"Listening to queue: {queueName}");
await using var client = new ServiceBusClient(connectionString);
ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions
{
AutoCompleteMessages = true
});
processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;
await processor.StartProcessingAsync();
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
await processor.StopProcessingAsync();
}
private static async Task MessageHandler(ProcessMessageEventArgs args)
{
string body = args.Message.Body.ToString();
string messageId = args.Message.MessageId;
Console.WriteLine($"Received message:");
Console.WriteLine($" MessageId: {messageId}");
Console.WriteLine($" Body: {body}");
Console.WriteLine($" -------------------------------------------------------------");
}
private static Task ErrorHandler(ProcessErrorEventArgs args)
{
Console.WriteLine($"Error: {args.Exception.Message}");
return Task.CompletedTask;
}
}
---------------------------------------------------------------------------------------------------------------------
Sender.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
class Sender
{
// Azure Service Bus connection details
private const string connectionString = "Connection string here";
private const string queueName = "queue name here";
static async Task Main(string[] args)
{
string directoryToWatch = @"C:\demos\servicebus\orderfiles"; // Change to your directory
string archiveDir = Path.Combine(directoryToWatch, "archive");
if (!Directory.Exists(archiveDir))
{
Directory.CreateDirectory(archiveDir);
}
Console.WriteLine($"Monitoring directory: {directoryToWatch}");
FileSystemWatcher watcher = new FileSystemWatcher(directoryToWatch, "*.xml");
watcher.Created += async (sender, e) ={angled bracket here} await OnNewFile(e.FullPath, archiveDir);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
private static async Task OnNewFile(string filePath, string archiveDir)
{
try
{
Console.WriteLine($"New file detected: {filePath}");
await Task.Delay(1000);
string xmlContent = await File.ReadAllTextAsync(filePath);
await SendMessageToQueue(xmlContent);
string destFileName = Path.Combine(archiveDir, Path.GetFileName(filePath));
File.Move(filePath, destFileName, overwrite: true);
Console.WriteLine($"File {Path.GetFileName(filePath)} sent and archived.");
}
catch (Exception ex)
{
Console.WriteLine($"Error processing file {filePath}: {ex.Message}");
}
}
private static async Task SendMessageToQueue(string messageBody)
{
await using var client = new ServiceBusClient(connectionString);
ServiceBusSender sender = client.CreateSender(queueName);
ServiceBusMessage message = new ServiceBusMessage(messageBody);
await sender.SendMessageAsync(message);
}
}
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: