-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServiceBusSessionReceiver.cs
More file actions
71 lines (58 loc) · 3.55 KB
/
Copy pathServiceBusSessionReceiver.cs
File metadata and controls
71 lines (58 loc) · 3.55 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="ServiceBusSessionProcessor"/> 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 ServiceBusSessionReceiver<TSubscriber> : ServiceBusReceiverBase<TSubscriber> where TSubscriber : ServiceBusSubscriberBase
{
private readonly ServiceBusSessionProcessor _processor;
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusSessionReceiver{TSubscriber}"/> class.
/// </summary>
/// <param name="client">The <see cref="ServiceBusClient"/>.</param>
/// <param name="options">The <see cref="ServiceBusSessionReceiverOptions"/>.</param>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public ServiceBusSessionReceiver(ServiceBusClient client, ServiceBusSessionReceiverOptions 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.CreateSessionProcessor(queueOrTopicName, Internal.GetValueFromConfigurationWhereApplicable(options.SubscriptionName!, config), options.SessionProcessorOptions)
: client.CreateSessionProcessor(queueOrTopicName, options.SessionProcessorOptions);
_processor.ProcessMessageAsync += OnProcessMessageAsync;
_processor.ProcessErrorAsync += OnProcessErrorAsync;
}
/// <summary>
/// Gets the <see cref="ServiceBusSessionReceiverOptions"/> to be used when creating the <see cref="ServiceBusSessionReceiver{TSubscriber}"/> instance.
/// </summary>
public new ServiceBusSessionReceiverOptions Options => (ServiceBusSessionReceiverOptions)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(ProcessSessionMessageEventArgs args) => ProcessMessageAsync(args.Message, new ProcessSessionMessageEventArgsActions(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);
}
}