-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBSender.cs
More file actions
92 lines (81 loc) · 2.85 KB
/
SBSender.cs
File metadata and controls
92 lines (81 loc) · 2.85 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
85
86
87
88
89
90
91
92
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
namespace ServiceBusPeekMessagesBug;
internal class SBSender
{
private readonly string _connectionString;
private readonly string _subscriptionName;
private readonly string _topicName;
private ServiceBusAdministrationClient _client;
private ServiceBusClient _clientSender;
private ServiceBusSender _serviceBusSender;
public SBSender(string connectionString, string topicName, string subscriptionName)
{
_connectionString = connectionString;
_topicName = topicName;
_subscriptionName = subscriptionName;
}
public async Task Send10TestMessages()
{
foreach (var i in Enumerable.Range(0, 10))
await _serviceBusSender.SendMessageAsync(new ServiceBusMessage(Guid.NewGuid().ToString())
{
SessionId = Guid.NewGuid().ToString()
});
}
public async Task Init()
{
/*
* This will create Topic with one subscription with default filter.
* Look at params.
*/
_client ??= new ServiceBusAdministrationClient(_connectionString);
var testTopic = await GetTopic();
if (testTopic == null)
await _client.CreateTopicAsync(new CreateTopicOptions(_topicName)
{
EnableBatchedOperations = true,
EnablePartitioning = true,
RequiresDuplicateDetection = false,
SupportOrdering = false,
Status = EntityStatus.Active,
AutoDeleteOnIdle = TimeSpan.FromMinutes(5) //Cleanup
});
var subscription = await GetSubscription();
if (subscription == null)
await _client.CreateSubscriptionAsync(new CreateSubscriptionOptions(_topicName, _subscriptionName)
{
EnableBatchedOperations = true,
EnableDeadLetteringOnFilterEvaluationExceptions = false,
DeadLetteringOnMessageExpiration = false,
RequiresSession = true
});
/*
* This will create simple sender.
*/
if (_clientSender?.IsClosed != false) _clientSender = new ServiceBusClient(_connectionString);
if (_serviceBusSender?.IsClosed != false) _serviceBusSender = _clientSender.CreateSender(_topicName);
}
private async Task<SubscriptionProperties> GetSubscription()
{
try
{
return (await _client.GetSubscriptionAsync(_topicName, _subscriptionName))?.Value;
}
catch (Exception e)
{
return null;
}
}
private async Task<TopicProperties> GetTopic()
{
try
{
return (await _client.GetTopicAsync(_topicName))?.Value;
}
catch (Exception e)
{
return null;
}
}
}