-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ProcessSessionEventArgs.cs
150 lines (132 loc) · 6.84 KB
/
ProcessSessionEventArgs.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Messaging.ServiceBus
{
/// <summary>
/// The <see cref="ProcessSessionEventArgs"/> contain event args related to the session being processed.
/// </summary>
public class ProcessSessionEventArgs : EventArgs
{
/// <summary>
/// A <see cref="System.Threading.CancellationToken"/> instance which will be
/// cancelled when <see cref="ServiceBusSessionProcessor.StopProcessingAsync"/>
/// is called, or when the session lock has been lost.
/// </summary>
public CancellationToken CancellationToken { get; }
/// <summary>
/// The <see cref="ServiceBusSessionReceiver"/> that will be used for setting and getting session state.
/// </summary>
private readonly ServiceBusSessionReceiver _sessionReceiver;
private readonly SessionReceiverManager _manager;
/// <summary>
/// The Session Id associated with the session being processed.
/// </summary>
public string SessionId => _sessionReceiver.SessionId;
/// <summary>
/// The identifier of the <see cref="ServiceBusSessionProcessor"/>.
/// </summary>
public string Identifier { get; }
/// <summary>
/// Gets the <see cref="DateTimeOffset"/> that the current session is locked until.
/// </summary>
public DateTimeOffset SessionLockedUntil => _sessionReceiver.SessionLockedUntil;
/// <summary>
/// The path of the Service Bus entity that the message was received from.
/// </summary>
public string EntityPath => _sessionReceiver.EntityPath;
/// <summary>
/// The fully qualified Service Bus namespace that the message was received from.
/// </summary>
public string FullyQualifiedNamespace => _sessionReceiver.FullyQualifiedNamespace;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSessionEventArgs"/> class.
/// </summary>
///
/// <param name="receiver">The <see cref="ServiceBusSessionReceiver"/> that will be used for all settlement methods
/// for the args.</param>
/// <param name="cancellationToken">The processor's <see cref="System.Threading.CancellationToken"/> instance which will be cancelled in the event that <see cref="ServiceBusProcessor.StopProcessingAsync"/> is called.
/// </param>
[EditorBrowsable(EditorBrowsableState.Never)]
public ProcessSessionEventArgs(
ServiceBusSessionReceiver receiver,
CancellationToken cancellationToken) : this(manager: null, cancellationToken)
{
_sessionReceiver = receiver;
}
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSessionEventArgs"/> class.
/// </summary>
///
/// <param name="receiver">The <see cref="ServiceBusSessionReceiver"/> that will be used for all settlement methods
/// for the args.</param>
/// <param name="identifier">The identifier of the processor.</param>
/// <param name="cancellationToken">The processor's <see cref="System.Threading.CancellationToken"/> instance which will be cancelled in the event that <see cref="ServiceBusProcessor.StopProcessingAsync"/> is called.
/// </param>
public ProcessSessionEventArgs(
ServiceBusSessionReceiver receiver,
string identifier,
CancellationToken cancellationToken) : this(receiver, cancellationToken)
{
Identifier = identifier;
}
internal ProcessSessionEventArgs(
SessionReceiverManager manager,
CancellationToken cancellationToken)
{
_manager = manager;
// manager would be null in scenarios where customers are using the public constructor for testing purposes.
_sessionReceiver = (ServiceBusSessionReceiver) _manager?.Receiver;
CancellationToken = cancellationToken;
}
internal ProcessSessionEventArgs(
SessionReceiverManager manager,
string identifier,
CancellationToken cancellationToken) : this(manager, cancellationToken)
{
Identifier = identifier;
}
/// <summary>
/// Gets the session state.
/// </summary>
///
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
///
/// <returns>The session state as <see cref="BinaryData"/>.</returns>
public virtual async Task<BinaryData> GetSessionStateAsync(
CancellationToken cancellationToken = default) =>
await _sessionReceiver.GetSessionStateAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Set a custom state on the session which can be later retrieved using <see cref="GetSessionStateAsync"/>
/// </summary>
///
/// <param name="sessionState">A <see cref="BinaryData"/> of session state</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
///
/// <remarks>This state is stored on Service Bus forever unless you set an empty state on it.</remarks>
///
/// <returns>A task to be resolved on when the operation has completed.</returns>
public virtual async Task SetSessionStateAsync(
BinaryData sessionState,
CancellationToken cancellationToken = default) =>
await _sessionReceiver.SetSessionStateAsync(sessionState, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Releases the session that is being processed. No receives will be initiated for the session and the
/// session will be closed.
/// The session may end up being reopened for processing immediately after closing if there are messages remaining in the session (
/// This depends on what other session messages may be in the queue or subscription).
/// </summary>
public virtual void ReleaseSession() =>
// manager will be null if instance created using the public constructor which is exposed for testing purposes
// This will be awaited when closing the receiver.
_ = _manager?.CancelAsync();
///<inheritdoc cref="ServiceBusSessionReceiver.RenewSessionLockAsync(CancellationToken)"/>
public virtual async Task RenewSessionLockAsync(CancellationToken cancellationToken = default)
{
await _sessionReceiver.RenewSessionLockAsync(cancellationToken).ConfigureAwait(false);
}
}
}