-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConsumingLoop.cs
46 lines (37 loc) · 1.27 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
// ReSharper disable ReplaceAsyncWithTaskReturn
public class ConsumingLoop
{
string connectionString = null!;
async Task ConsumeLoop()
{
#region ConsumeLoop
static async Task Callback(
SqlTransaction transaction,
IncomingMessage message,
Cancel cancel)
{
if (message.Body != null)
{
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync(cancel);
Console.WriteLine($"Reply received:\r\n{bodyText}");
}
}
Task<SqlTransaction> BuildTransaction(Cancel cancel) =>
ConnectionHelpers.BeginTransaction(connectionString, cancel);
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: BuildTransaction,
callback: Callback,
errorCallback: ErrorCallback);
consumingLoop.Start();
// stop consuming
await consumingLoop.Stop();
#endregion
}
}