forked from NServiceBusExtensions/NServiceBus.SqlNative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsumingLoop.cs
53 lines (45 loc) · 1.45 KB
/
ConsumingLoop.cs
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
52
53
using System;
using System.Data.Common;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.Transport.SqlServerNative;
public class ConsumingLoop
{
string connectionString = null!;
async Task ConsumeLoop()
{
#region ConsumeLoop
static async Task Callback(
DbTransaction transaction,
IncomingMessage message,
CancellationToken cancellation)
{
if (message.Body != null)
{
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync();
Console.WriteLine($"Reply received:\r\n{bodyText}");
}
}
Task<DbTransaction> TransactionBuilder(CancellationToken cancellation)
{
return ConnectionHelpers.BeginTransaction(connectionString, cancellation);
}
static void ErrorCallback(Exception exception)
{
Environment.FailFast("Message consuming loop failed", exception);
}
// start consuming
var consumingLoop = new MessageConsumingLoop(
table: "endpointTable",
delay: TimeSpan.FromSeconds(1),
transactionBuilder: TransactionBuilder,
callback: Callback,
errorCallback: ErrorCallback);
consumingLoop.Start();
// stop consuming
await consumingLoop.Stop();
#endregion
}
}