-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceBus.cs
More file actions
51 lines (44 loc) · 1.58 KB
/
ServiceBus.cs
File metadata and controls
51 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Microsoft.Azure.ServiceBus;
using RoodFluweel.Printing.Model;
public class ServiceBus
{
static QueueClient? queueClient;
public static void ReceiveMessages(string ConnectionString, string QueueName, Func<TicketBundle, Task> messageHandler)
{
// Initialize the queue client
queueClient = new QueueClient(ConnectionString, QueueName);
// Register the message handler and receive messages in a loop
queueClient.RegisterMessageHandler(async (message, token) =>
{
// the old way
// TicketBundle tb = MessageInteropExtensions.GetBody<TicketBundle>(message);
// the new way, we use Json now.
TicketBundle? tb = System.Text.Json.JsonSerializer.Deserialize<TicketBundle>(message.Body);
if (tb != null)
{
await messageHandler(tb);
}
// Complete the message so that it is not received again
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
},
new MessageHandlerOptions(args =>
{
// Handle any exceptions
Console.WriteLine($"Exception: {args.Exception}");
return Task.CompletedTask;
})
{
MaxConcurrentCalls = 1,
AutoComplete = false // We'll manually complete the message after processing
}
);
}
public static async Task StopReceivingMessagesAsync()
{
// Close the queue client
if (queueClient != null)
{
await queueClient.CloseAsync();
}
}
}