« Azure Service Bus » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(→Sender) |
(→Sender) |
||
Ligne 26 : | Ligne 26 : | ||
var message = new ServiceBusMessage("Hello!"); | var message = new ServiceBusMessage("Hello!"); | ||
await sender.SendMessageAsync(message); | await sender.SendMessageAsync(message); | ||
Console.WriteLine($"Sent {message.Body}"); | |||
} | } | ||
finally | finally |
Version du 15 décembre 2021 à 17:39
Description
It is a message broker with message queues and publish-subscribe topics.
Configure the service bus on Azure
- Azure portal → Service Bus → Add
- Create namespace: the namespace will be the url use by the service bus
- Once deployed, go to the newly create service bus namespace → add queue
Sender
Add Nuget package Azure.Messaging.ServiceBus
Program.cs |
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; private const string QueueName = "myqueue"; private const string TopicName = "topic1"; static async Task Main(string[] args) { var client = new ServiceBusClient(ConnectionString); try // use await using in C# 8 / .Net Core { var sender = client.CreateSender(QueueName); var sender = client.CreateSender(TopicName); var message = new ServiceBusMessage("Hello!"); await sender.SendMessageAsync(message); Console.WriteLine($"Sent {message.Body}"); } finally { await client.DisposeAsync(); } } |
Receiver
Add Nuget package Azure.Messaging.ServiceBus
Program.cs |
private const string ConnectionString = "get it from Azure Portal → Service Bus Namespace → Shared access policy → Primary Connection String"; private const string QueueName = "myqueue"; private const string TopicName = "topic1"; static async Task Main(string[] args) { var client = new ServiceBusClient(ConnectionString); try // use await using in C# 8 / .Net Core { var receiver = client.CreateReceiver(QueueName); var message = await receiver.ReceiveMessageAsync(); Console.WriteLine($"Received {message.Body}"); } finally { await client.DisposeAsync(); } } |