@@ -20,14 +20,15 @@ sys.correlationid like 'abc-%'
20
20
21
21
> [! NOTE ]
22
22
> - For a list of system properties , see [Messages , payloads , and serialization ](service - bus - messages - payloads .md ).
23
- > - Use system property names from [Microsoft .Azure .ServiceBus .Message ](/ dotnet / api / microsoft .azure .servicebus .message #properties ) in your filters even when you use [ServiceBusMessage ](/ dotnet / api / azure .messaging .servicebus .servicebusmessage ) from the new [Azure .Messaging .ServiceBus ](/ dotnet / api / azure .messaging .servicebus ) namespace to send and receive messages . The `Subject ` from [ServiceBusMessage ](/dotnet /api /azure .messaging .servicebus .servicebusmessage ) maps to `Label ` in [Microsoft .Azure .ServiceBus .Message ](/dotnet /api /microsoft .azure .servicebus .message #properties ).
23
+ > - Use system property names from [Microsoft .Azure .ServiceBus .Message ](/ dotnet / api / microsoft .azure .servicebus .message #properties ) in your filters even when you use [ServiceBusMessage ](/ dotnet / api / azure .messaging .servicebus .servicebusmessage ) from the new [Azure .Messaging .ServiceBus ](/ dotnet / api / azure .messaging .servicebus ) namespace to send and receive messages .
24
+ > - `Subject ` from [Azure .Messaging .ServiceBus .ServiceBusMessage ](/dotnet /api /azure .messaging .servicebus .servicebusmessage ) maps to `Label ` in [Microsoft .Azure .ServiceBus .Message ](/dotnet /api /microsoft .azure .servicebus .message #properties ).
24
25
25
26
## Filter on message properties
26
- Here are the examples of using message properties in a filter . You can access message properties using `user .property -name ` or just `property -name `.
27
+ Here are the examples of using application or user properties in a filter . You can access application properties set by using [ Azure . Messaging . ServiceBus . ServiceBusMessage . ApplicationProperties ](/ dotnet / api / azure . messaging . servicebus . servicebusmessage . applicationproperties )) ( latest ) or user properties set by [ Microsoft . Azure . ServiceBus . Message . UserProperty ](/ dotnet / api / microsoft . azure . servicebus . message . userproperties ) ( deprecated ) using the syntax : `user .property -name ` or just `property -name `.
27
28
28
29
```csharp
29
30
MessageProperty = 'A '
30
- SuperHero like 'SuperMan %'
31
+ user . SuperHero like 'SuperMan %'
31
32
```
32
33
33
34
## Filter on message properties with special characters
@@ -98,7 +99,236 @@ filter.Properties["color"] = "Red";
98
99
It's equivalent to:
` sys.ReplyTo = '[email protected] ' AND sys.Label = 'Important' AND color = 'Red' `
99
100
100
101
102
+ ## .NET example for creating subscription filters
103
+ Here's a .NET C# example that creates the following Service Bus entities:
101
104
105
+ - Service Bus topic named ` topicfiltersampletopic `
106
+ - Subscription to the topic named ` AllOrders ` with a SQL filter expression ` 1=1 `
107
+ - Subscription named ` ColorBlueSize10Orders ` with a SQL filter expression ` color='blue' AND quantity=10 `
108
+ - Subscription named ` ColorRed ` with a SQL filter expression ` color='red' ` and an action
109
+ - Subscription named ` HighPriorityRedOrders ` with a correlation filter expression ` Subject = "red", CorrelationId = "high" `
110
+
111
+ See the inline code comments for more details.
112
+
113
+ ``` csharp
114
+ namespace CreateTopicsAndSubscriptionsWithFilters
115
+ {
116
+ using Azure .Messaging .ServiceBus .Administration ;
117
+ using System ;
118
+ using System .Threading .Tasks ;
119
+
120
+ public class Program
121
+ {
122
+ // Service Bus Administration Client object to create topics and subscriptions
123
+ static ServiceBusAdministrationClient adminClient ;
124
+
125
+ // connection string to the Service Bus namespace
126
+ static readonly string connectionString = " <YOUR SERVICE BUS NAMESPACE - CONNECTION STRING>" ;
127
+
128
+ // name of the Service Bus topic
129
+ static readonly string topicName = " topicfiltersampletopic" ;
130
+
131
+ // names of subscriptions to the topic
132
+ static readonly string subscriptionAllOrders = " AllOrders" ;
133
+ static readonly string subscriptionColorBlueSize10Orders = " ColorBlueSize10Orders" ;
134
+ static readonly string subscriptionColorRed = " ColorRed" ;
135
+ static readonly string subscriptionHighPriorityRedOrders = " HighPriorityRedOrders" ;
136
+
137
+ public static async Task Main ()
138
+ {
139
+ try
140
+ {
141
+
142
+ Console .WriteLine (" Creating the Service Bus Administration Client object" );
143
+ adminClient = new ServiceBusAdministrationClient (connectionString );
144
+
145
+ Console .WriteLine ($" Creating the topic {topicName }" );
146
+ await adminClient .CreateTopicAsync (topicName );
147
+
148
+ Console .WriteLine ($" Creating the subscription {subscriptionAllOrders } for the topic with a SQL filter " );
149
+ // Create a SQL filter with an expression that always evaluates to true
150
+ await adminClient .CreateSubscriptionAsync (
151
+ new CreateSubscriptionOptions (topicName , subscriptionAllOrders ),
152
+ new CreateRuleOptions (" AllOrders" ,new SqlRuleFilter (" 1=1" )));
153
+
154
+ Console .WriteLine ($" Creating the subscription {subscriptionColorBlueSize10Orders } with a SQL filter" );
155
+ // Create a SQL filter with color set to blue and quantity to 10
156
+ await adminClient .CreateSubscriptionAsync (
157
+ new CreateSubscriptionOptions (topicName , subscriptionColorBlueSize10Orders ),
158
+ new CreateRuleOptions (" BlueSize10Orders" , new SqlRuleFilter (" color='blue' AND quantity=10" )));
159
+
160
+ Console .WriteLine ($" Creating the subscription {subscriptionColorRed } with a SQL filter" );
161
+ // Create a SQL filter with color equals to red and a SQL action with a set of statements
162
+ await adminClient .CreateSubscriptionAsync (topicName , subscriptionColorRed );
163
+ // remove the $Default rule
164
+ await adminClient .DeleteRuleAsync (topicName , subscriptionColorRed , " $Default" );
165
+ // now create the new rule. notice that user. prefix is used for the user/application property
166
+ await adminClient .CreateRuleAsync (topicName , subscriptionColorRed , new CreateRuleOptions
167
+ {
168
+ Name = " RedOrdersWithAction" ,
169
+ Filter = new SqlRuleFilter (" user.color='red'" ),
170
+ Action = new SqlRuleAction (" SET quantity = quantity / 2; REMOVE priority;SET sys.CorrelationId = 'low';" )
171
+
172
+ }
173
+ );
174
+
175
+ Console .WriteLine ($" Creating the subscription {subscriptionHighPriorityRedOrders } with a correlation filter" );
176
+ // Create a correlation filter with color set to Red and priority set to High
177
+ await adminClient .CreateSubscriptionAsync (
178
+ new CreateSubscriptionOptions (topicName , subscriptionHighPriorityRedOrders ),
179
+ new CreateRuleOptions (" HighPriorityRedOrders" , new CorrelationRuleFilter () {Subject = " red" , CorrelationId = " high" } ));
180
+
181
+ // delete resources
182
+ // await adminClient.DeleteTopicAsync(topicName);
183
+ }
184
+ catch (Exception e )
185
+ {
186
+ Console .WriteLine (e .ToString ());
187
+ }
188
+ }
189
+ }
190
+ }
191
+ ```
192
+
193
+ ## .NET example for sending receiving messages
194
+
195
+ ``` csharp
196
+ namespace SendAndReceiveMessages
197
+ {
198
+ using System ;
199
+ using System .Text ;
200
+ using System .Threading .Tasks ;
201
+ using Azure .Messaging .ServiceBus ;
202
+ using Newtonsoft .Json ;
203
+
204
+ public class Program
205
+ {
206
+ const string TopicName = " TopicFilterSampleTopic" ;
207
+ const string SubscriptionAllMessages = " AllOrders" ;
208
+ const string SubscriptionColorBlueSize10Orders = " ColorBlueSize10Orders" ;
209
+ const string SubscriptionColorRed = " ColorRed" ;
210
+ const string SubscriptionHighPriorityOrders = " HighPriorityRedOrders" ;
211
+
212
+ // connection string to your Service Bus namespace
213
+ static string connectionString = " <YOUR SERVICE BUS NAMESPACE - CONNECTION STRING>" ;
214
+
215
+ // the client that owns the connection and can be used to create senders and receivers
216
+ static ServiceBusClient client ;
217
+
218
+ // the sender used to publish messages to the topic
219
+ static ServiceBusSender sender ;
220
+
221
+ // the receiver used to receive messages from the subscription
222
+ static ServiceBusReceiver receiver ;
223
+
224
+ public async Task SendAndReceiveTestsAsync (string connectionString )
225
+ {
226
+ // This sample demonstrates how to use advanced filters with ServiceBus topics and subscriptions.
227
+ // The sample creates a topic and 3 subscriptions with different filter definitions.
228
+ // Each receiver will receive matching messages depending on the filter associated with a subscription.
229
+
230
+ // Send sample messages.
231
+ await this .SendMessagesToTopicAsync (connectionString );
232
+
233
+ // Receive messages from subscriptions.
234
+ await this .ReceiveAllMessageFromSubscription (connectionString , SubscriptionAllMessages );
235
+ await this .ReceiveAllMessageFromSubscription (connectionString , SubscriptionColorBlueSize10Orders );
236
+ await this .ReceiveAllMessageFromSubscription (connectionString , SubscriptionColorRed );
237
+ await this .ReceiveAllMessageFromSubscription (connectionString , SubscriptionHighPriorityOrders );
238
+ }
239
+
240
+
241
+ async Task SendMessagesToTopicAsync (string connectionString )
242
+ {
243
+ // Create the clients that we'll use for sending and processing messages.
244
+ client = new ServiceBusClient (connectionString );
245
+ sender = client .CreateSender (TopicName );
246
+
247
+ Console .WriteLine (" \n Sending orders to topic." );
248
+
249
+ // Now we can start sending orders.
250
+ await Task .WhenAll (
251
+ SendOrder (sender , new Order ()),
252
+ SendOrder (sender , new Order { Color = " blue" , Quantity = 5 , Priority = " low" }),
253
+ SendOrder (sender , new Order { Color = " red" , Quantity = 10 , Priority = " high" }),
254
+ SendOrder (sender , new Order { Color = " yellow" , Quantity = 5 , Priority = " low" }),
255
+ SendOrder (sender , new Order { Color = " blue" , Quantity = 10 , Priority = " low" }),
256
+ SendOrder (sender , new Order { Color = " blue" , Quantity = 5 , Priority = " high" }),
257
+ SendOrder (sender , new Order { Color = " blue" , Quantity = 10 , Priority = " low" }),
258
+ SendOrder (sender , new Order { Color = " red" , Quantity = 5 , Priority = " low" }),
259
+ SendOrder (sender , new Order { Color = " red" , Quantity = 10 , Priority = " low" }),
260
+ SendOrder (sender , new Order { Color = " red" , Quantity = 5 , Priority = " low" }),
261
+ SendOrder (sender , new Order { Color = " yellow" , Quantity = 10 , Priority = " high" }),
262
+ SendOrder (sender , new Order { Color = " yellow" , Quantity = 5 , Priority = " low" }),
263
+ SendOrder (sender , new Order { Color = " yellow" , Quantity = 10 , Priority = " low" })
264
+ );
265
+
266
+ Console .WriteLine (" All messages sent." );
267
+ }
268
+
269
+ async Task SendOrder (ServiceBusSender sender , Order order )
270
+ {
271
+ var message = new ServiceBusMessage (Encoding .UTF8 .GetBytes (JsonConvert .SerializeObject (order )))
272
+ {
273
+ CorrelationId = order .Priority ,
274
+ Subject = order .Color ,
275
+ ApplicationProperties =
276
+ {
277
+ { " color" , order .Color },
278
+ { " quantity" , order .Quantity },
279
+ { " priority" , order .Priority }
280
+ }
281
+ };
282
+ await sender .SendMessageAsync (message );
283
+
284
+ Console .WriteLine (" Sent order with Color={0}, Quantity={1}, Priority={2}" , order .Color , order .Quantity , order .Priority );
285
+ }
286
+
287
+ async Task ReceiveAllMessageFromSubscription (string connectionString , string subsName )
288
+ {
289
+ var receivedMessages = 0 ;
290
+
291
+ receiver = client .CreateReceiver (TopicName , subsName , new ServiceBusReceiverOptions () { ReceiveMode = ServiceBusReceiveMode .ReceiveAndDelete } );
292
+
293
+ // Create a receiver from the subscription client and receive all messages.
294
+ Console .WriteLine (" \n Receiving messages from subscription {0}." , subsName );
295
+
296
+ while (true )
297
+ {
298
+ var receivedMessage = await receiver .ReceiveMessageAsync (TimeSpan .FromSeconds (10 ));
299
+ if (receivedMessage != null )
300
+ {
301
+ foreach (var prop in receivedMessage .ApplicationProperties )
302
+ {
303
+ Console .Write (" {0}={1}," , prop .Key , prop .Value );
304
+ }
305
+ Console .WriteLine (" CorrelationId={0}" , receivedMessage .CorrelationId );
306
+ receivedMessages ++ ;
307
+ }
308
+ else
309
+ {
310
+ // No more messages to receive.
311
+ break ;
312
+ }
313
+ }
314
+ Console .WriteLine (" Received {0} messages from subscription {1}." , receivedMessages , subsName );
315
+ }
316
+
317
+ public static async Task Main ()
318
+ {
319
+ try
320
+ {
321
+ Program app = new Program ();
322
+ await app .SendAndReceiveTestsAsync (connectionString );
323
+ }
324
+ catch (Exception e )
325
+ {
326
+ Console .WriteLine (e .ToString ());
327
+ }
328
+ }
329
+ }
330
+ }
331
+ ```
102
332
103
333
104
334
## Next steps
0 commit comments