-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServiceBusReceiver.cs
More file actions
71 lines (58 loc) · 3.42 KB
/
Copy pathServiceBusReceiver.cs
File metadata and controls
71 lines (58 loc) · 3.42 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
namespace CoreEx.Azure.Messaging.ServiceBus;
/// <summary>
/// Encapsulates the <see cref="ServiceBusProcessor"/> lifetime and message receiving management.
/// </summary>
/// <typeparam name="TSubscriber">The <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/> to be used for receiving each <see cref="ServiceBusReceivedMessage"/>.</typeparam>
public sealed class ServiceBusReceiver<TSubscriber> : ServiceBusReceiverBase<TSubscriber> where TSubscriber : ServiceBusSubscriberBase
{
private readonly ServiceBusProcessor _processor;
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusReceiver{TSubscriber}"/> class.
/// </summary>
/// <param name="client">The <see cref="ServiceBusClient"/>.</param>
/// <param name="options">The <see cref="ServiceBusReceiverOptions"/>.</param>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public ServiceBusReceiver(ServiceBusClient client, ServiceBusReceiverOptions options, IServiceProvider serviceProvider, ILogger<ServiceBusReceiver<TSubscriber>> logger)
: base(client, options, serviceProvider, logger)
{
var config = serviceProvider.GetRequiredService<IConfiguration>();
var queueOrTopicName = Internal.GetValueFromConfigurationWhereApplicable(options.QueueOrTopicName, config);
_processor = options.IsSubscription
? client.CreateProcessor(queueOrTopicName, Internal.GetValueFromConfigurationWhereApplicable(options.SubscriptionName!, config), options.ProcessorOptions)
: client.CreateProcessor(queueOrTopicName, options.ProcessorOptions);
_processor.ProcessMessageAsync += OnProcessMessageAsync;
_processor.ProcessErrorAsync += OnProcessErrorAsync;
}
/// <summary>
/// Gets the <see cref="ServiceBusReceiverOptions"/> to be used when creating the <see cref="ServiceBusReceiver"/> instance.
/// </summary>
public new ServiceBusReceiverOptions Options => (ServiceBusReceiverOptions)base.Options;
/// <inheritdoc/>
protected override Task OnStartAsync(CancellationToken cancellationToken) => _processor.StartProcessingAsync(cancellationToken);
/// <inheritdoc/>
protected override Task OnPauseAsync(CancellationToken cancellationToken) => _processor.StopProcessingAsync(cancellationToken);
/// <inheritdoc/>
protected override Task OnResumeAsync(CancellationToken cancellationToken) => _processor.StartProcessingAsync(cancellationToken);
/// <inheritdoc/>
protected override Task OnStopAsync(CancellationToken cancellationToken) => _processor.StopProcessingAsync(cancellationToken);
/// <summary>
/// Handles the processing of a message.
/// </summary>
private Task OnProcessMessageAsync(ProcessMessageEventArgs args) => ProcessMessageAsync(args.Message, new ProcessMessageEventArgsActions(args), args.CancellationToken);
/// <summary>
/// Handles the processing of an error/exception.
/// </summary>
private Task OnProcessErrorAsync(ProcessErrorEventArgs args)
{
ServiceBusErrorClassifier.ClassifyAndLogError(Logger, args);
return Task.CompletedTask;
}
/// <inheritdoc/>
protected async override ValueTask DisposeAsync(bool disposing)
{
if (disposing)
await _processor.DisposeAsync().ConfigureAwait(false);
await base.DisposeAsync(disposing).ConfigureAwait(false);
}
}