-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQueueProcessor.cs
More file actions
84 lines (68 loc) · 2.26 KB
/
QueueProcessor.cs
File metadata and controls
84 lines (68 loc) · 2.26 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackifyLib;
namespace WindowsService1
{
class QueueProcessor
{
private QueueClient _QueueClient = null;
public void Start()
{
OnMessageOptions options = new OnMessageOptions
{
MaxConcurrentCalls = 5,
AutoComplete = false
};
try
{
_QueueClient = QueueClient.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"], "WindowsService1");
_QueueClient.OnMessageAsync(async m =>
{
bool shouldAbandon = false;
try
{
//Create a new ID for the operation for correlation
var tracer = ProfileTracer.CreateAsOperation("Queue.WindowsService1", Trace.CorrelationManager.ActivityId.ToString());
// asynchronouse processing of messages
await tracer.ExecAsync(() =>
{
return ProcessMessageAsync(m);
});
// complete if successful processing
await m.CompleteAsync();
}
catch (Exception ex)
{
shouldAbandon = true;
Console.WriteLine(ex);
}
if (shouldAbandon)
{
await m.AbandonAsync();
}
},
options);
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
private async Task<bool> ProcessMessageAsync(BrokeredMessage message)
{
Debug.WriteLine("Recieved message " + message.MessageId);
Debug.WriteLine("Do some cool back end processing here");
return true;
}
public void Stop()
{
_QueueClient.Close();
}
}
}