1+ namespace SlimMessageBus . Host . AzureServiceBus ;
2+
3+ using Azure . Messaging . ServiceBus ;
4+ using Azure . Messaging . ServiceBus . Administration ;
5+
6+ public static class AsbConsumerBuilderExtensions
7+ {
8+ /// <summary>
9+ /// Sets the queue name for this consumer to use.
10+ /// </summary>
11+ /// <typeparam name="TConsumerBuilder"></typeparam>
12+ /// <param name="builder"></param>
13+ /// <param name="queue"></param>
14+ /// <param name="queueConfig"></param>
15+ /// <returns></returns>
16+ /// <exception cref="ArgumentNullException"></exception>
17+ public static TConsumerBuilder Queue < TConsumerBuilder > ( this TConsumerBuilder builder , string queue , Action < TConsumerBuilder > queueConfig = null )
18+ where TConsumerBuilder : AbstractConsumerBuilder
19+ {
20+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
21+ if ( queue is null ) throw new ArgumentNullException ( nameof ( queue ) ) ;
22+
23+ builder . ConsumerSettings . Path = queue ;
24+ builder . ConsumerSettings . PathKind = PathKind . Queue ;
25+
26+ queueConfig ? . Invoke ( builder ) ;
27+
28+ return builder ;
29+ }
30+
31+ private static void AssertIsTopicForSubscriptionName ( AbstractConsumerSettings settings )
32+ {
33+ if ( settings is null ) throw new ArgumentNullException ( nameof ( settings ) ) ;
34+
35+ if ( settings . PathKind == PathKind . Queue )
36+ {
37+ var methodName = $ ".{ nameof ( SubscriptionName ) } (...)";
38+
39+ var messageType = settings is ConsumerSettings consumerSettings
40+ ? consumerSettings . MessageType . FullName
41+ : string . Empty ;
42+
43+ throw new ConfigurationMessageBusException ( $ "The subscription name configuration ({ methodName } ) does not apply to Azure ServiceBus queues (it only applies to topic consumers). Remove the { methodName } configuration for type { messageType } and queue { settings . Path } or change the consumer configuration to consume from topic { settings . Path } instead.") ;
44+ }
45+ }
46+
47+ /// <summary>
48+ /// Configures the subscription name when consuming form Azure ServiceBus topic.
49+ /// Not applicable when consuming from Azure ServiceBus queue.
50+ /// </summary>
51+ /// <param name="builder"></param>
52+ /// <param name="subscriptionName"></param>
53+ /// <returns></returns>
54+ public static T SubscriptionName < T > ( this T builder , string subscriptionName )
55+ where T : IAbstractConsumerBuilder
56+ {
57+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
58+ if ( subscriptionName is null ) throw new ArgumentNullException ( nameof ( subscriptionName ) ) ;
59+
60+ AssertIsTopicForSubscriptionName ( builder . ConsumerSettings ) ;
61+
62+ builder . ConsumerSettings . SetSubscriptionName ( subscriptionName ) ;
63+ return builder ;
64+ }
65+
66+ /// <summary>
67+ /// Azure Service Bus consumer setting. See underlying client for more details: https://docs.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus.servicebusprocessoroptions.maxautolockrenewalduration
68+ /// </summary>
69+ /// <typeparam name="TConsumerBuilder"></typeparam>
70+ /// <param name="builder"></param>
71+ /// <param name="duration"></param>
72+ /// <returns></returns>
73+ /// <exception cref="ArgumentNullException"></exception>
74+ public static TConsumerBuilder MaxAutoLockRenewalDuration < TConsumerBuilder > ( this TConsumerBuilder builder , TimeSpan duration )
75+ where TConsumerBuilder : IAbstractConsumerBuilder
76+ {
77+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
78+
79+ builder . ConsumerSettings . SetMaxAutoLockRenewalDuration ( duration ) ;
80+
81+ return builder ;
82+ }
83+
84+ /// <summary>
85+ /// Azure Service Bus consumer setting. See underlying client for more details: https://docs.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus.servicebusprocessoroptions.subqueue
86+ /// </summary>
87+ /// <typeparam name="TConsumerBuilder"></typeparam>
88+ /// <param name="builder"></param>
89+ /// <param name="subQueue"></param>
90+ /// <returns></returns>
91+ /// <exception cref="ArgumentNullException"></exception>
92+ public static TConsumerBuilder SubQueue < TConsumerBuilder > ( this TConsumerBuilder builder , SubQueue subQueue )
93+ where TConsumerBuilder : IAbstractConsumerBuilder
94+ {
95+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
96+
97+ builder . ConsumerSettings . SetSubQueue ( subQueue ) ;
98+
99+ return builder ;
100+ }
101+
102+ /// <summary>
103+ /// Azure Service Bus consumer setting. See underlying client for more details: https://docs.microsoft.com/en-us/dotnet/api/azure.messaging.servicebus.servicebusprocessoroptions.prefetchcount
104+ /// </summary>
105+ /// <typeparam name="TConsumerBuilder"></typeparam>
106+ /// <param name="builder"></param>
107+ /// <param name="prefetchCount "></param>
108+ /// <returns></returns>
109+ /// <exception cref="ArgumentNullException"></exception>
110+ public static TConsumerBuilder PrefetchCount < TConsumerBuilder > ( this TConsumerBuilder builder , int prefetchCount )
111+ where TConsumerBuilder : IAbstractConsumerBuilder
112+ {
113+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
114+
115+ builder . ConsumerSettings . SetPrefetchCount ( prefetchCount ) ;
116+
117+ return builder ;
118+ }
119+
120+ /// <summary>
121+ /// Enables Azue Service Bus session support for this consumer
122+ /// </summary>
123+ /// <typeparam name="TConsumerBuilder"></typeparam>
124+ /// <param name="builder"></param>
125+ /// <returns></returns>
126+ /// <exception cref="ArgumentNullException"></exception>
127+ public static TConsumerBuilder EnableSession < TConsumerBuilder > ( this TConsumerBuilder builder , Action < AsbConsumerSessionBuilder > sessionConfiguration = null )
128+ where TConsumerBuilder : IAbstractConsumerBuilder
129+ {
130+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
131+
132+ builder . ConsumerSettings . SetEnableSession ( true ) ;
133+
134+ if ( sessionConfiguration != null )
135+ {
136+ sessionConfiguration ( new AsbConsumerSessionBuilder ( builder . ConsumerSettings ) ) ;
137+ }
138+
139+ return builder ;
140+ }
141+
142+ /// <summary>
143+ /// Adds a named SQL filter to the subscription (Azure Service Bus). Setting relevant only if topology provisioning enabled.
144+ /// </summary>
145+ /// <typeparam name="TConsumerBuilder"></typeparam>
146+ /// <param name="builder"></param>
147+ /// <param name="ruleName">The name of the filter</param>
148+ /// <param name="filterSql">The SQL expression of the filter</param>
149+ /// <param name="actionSql">The action to be performed on the filter</param>
150+ /// <returns></returns>
151+ /// <exception cref="ArgumentNullException"></exception>
152+ public static TConsumerBuilder SubscriptionSqlFilter < TConsumerBuilder > ( this TConsumerBuilder builder , string filterSql , string ruleName = "default" , string actionSql = null )
153+ where TConsumerBuilder : IAbstractConsumerBuilder
154+ {
155+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
156+
157+ var filterByName = builder . ConsumerSettings . GetRules ( createIfNotExists : true ) ;
158+ filterByName [ ruleName ] = new SubscriptionSqlRule { Name = ruleName , SqlFilter = filterSql , SqlAction = actionSql } ;
159+
160+ return builder ;
161+ }
162+
163+ /// <summary>
164+ /// <see cref="CreateQueueOptions"/> when the ASB queue does not exist and needs to be created
165+ /// </summary>
166+ /// <typeparam name="TConsumerBuilder"></typeparam>
167+ /// <param name="builder"></param>
168+ /// <returns></returns>
169+ public static TConsumerBuilder CreateQueueOptions < TConsumerBuilder > ( this TConsumerBuilder builder , Action < CreateQueueOptions > action )
170+ where TConsumerBuilder : IAbstractConsumerBuilder
171+ {
172+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
173+ if ( action is null ) throw new ArgumentNullException ( nameof ( action ) ) ;
174+
175+ builder . ConsumerSettings . SetQueueOptions ( action ) ;
176+ return builder ;
177+ }
178+
179+ /// <summary>
180+ /// <see cref="CreateTopicOptions"/> when the ASB topic does not exist and needs to be created
181+ /// </summary>
182+ /// <typeparam name="TConsumerBuilder"></typeparam>
183+ /// <param name="builder"></param>
184+ /// <param name="action"></param>
185+ /// <returns></returns>
186+ public static TConsumerBuilder CreateTopicOptions < TConsumerBuilder > ( this TConsumerBuilder builder , Action < CreateTopicOptions > action )
187+ where TConsumerBuilder : IAbstractConsumerBuilder
188+ {
189+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
190+ if ( action is null ) throw new ArgumentNullException ( nameof ( action ) ) ;
191+
192+ builder . ConsumerSettings . SetTopicOptions ( action ) ;
193+ return builder ;
194+ }
195+
196+ /// <summary>
197+ /// <see cref="CreateSubscriptionOptions"/> when the ASB subscription does not exist and needs to be created
198+ /// </summary>
199+ /// <typeparam name="TConsumerBuilder"></typeparam>
200+ /// <param name="builder"></param>
201+ /// <param name="action"></param>
202+ /// <returns></returns>
203+ public static TConsumerBuilder CreateSubscriptionOptions < TConsumerBuilder > ( this TConsumerBuilder builder , Action < CreateSubscriptionOptions > action )
204+ where TConsumerBuilder : IAbstractConsumerBuilder
205+ {
206+ if ( builder is null ) throw new ArgumentNullException ( nameof ( builder ) ) ;
207+ if ( action is null ) throw new ArgumentNullException ( nameof ( action ) ) ;
208+
209+ builder . ConsumerSettings . SetSubscriptionOptions ( action ) ;
210+ return builder ;
211+ }
212+ }
0 commit comments