public async Task FalseRuleFilter()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesDescription.First().Name);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new FalseRuleFilter(),
                    Name   = "BooleanFilter"
                });

                rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual("BooleanFilter", rulesDescription.First().Name);

                await SendMessages(client, scope.TopicName);

                var receiver = client.CreateReceiver(scope.TopicName, scope.SubscriptionNames.First());
                var messages = await receiver.ReceiveMessagesAsync(Orders.Length, TimeSpan.FromSeconds(10));

                Assert.AreEqual(0, messages.Count());
            }
        }
        public async Task ThrowIfAddSameRuleNameTwice()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new CorrelationRuleFilter {
                        Subject = "yellow"
                    },
                    Name = "CorrelationRuleFilter"
                });

                Assert.That(async() => await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new CorrelationRuleFilter {
                        Subject = "red"
                    },
                    Name = "CorrelationRuleFilter"
                }), Throws.InstanceOf <ServiceBusException>());
            }
        }
Пример #3
0
        public async Task ManyRulesOnSubscription(int count)
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                List <RuleProperties> rulesProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(1, rulesProperties.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesProperties.First().Name);

                await ruleManager.DeleteRuleAsync(RuleProperties.DefaultRuleName);

                List <string> ruleNames = new();
                for (int i = 0; i < count; i++)
                {
                    var ruleName = $"CorrelationUserPropertyRule-{i}";
                    ruleNames.Add(ruleName);
                    await ruleManager.CreateRuleAsync(new CreateRuleOptions
                    {
                        Filter = new CorrelationRuleFilter {
                            ApplicationProperties = { { "color", "red" } }
                        },
                        Name = ruleName
                    });
                }

                ruleNames.Sort();
                rulesProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(count, rulesProperties.Count);

                // the rules are returned in name order
                for (int i = 0; i < count; i++)
                {
                    Assert.AreEqual(ruleNames[i], rulesProperties[i].Name);
                }

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.Where(c => c.Color == "red").ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
        public async Task CorrelationRuleFilterWithSqlRuleAction()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesDescription.First().Name);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new CorrelationRuleFilter {
                        ApplicationProperties = { { "Color", "blue" } }
                    },
                    Action = new SqlRuleAction("Set Priority = 'high'"),
                    Name   = "CorrelationRuleWithAction"
                });

                rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual("CorrelationRuleWithAction", rulesDescription.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders   = Orders.Where(c => c.Color == "blue").ToList();
                var receivedMessages = await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);

                foreach (var message in receivedMessages)
                {
                    Assert.AreEqual("high", message.ApplicationProperties["priority"], "Priority of the receivedMessage is different than expected");
                }
            }
        }
Пример #5
0
        public async Task TrueRuleFilter()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                List <RuleProperties> ruleProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(1, ruleProperties.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, ruleProperties.First().Name);

                await ruleManager.DeleteRuleAsync(RuleProperties.DefaultRuleName);

                ruleProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(0, ruleProperties.Count());

                await ruleManager.CreateRuleAsync(new CreateRuleOptions
                {
                    Filter = new TrueRuleFilter(),
                    Name   = "BooleanFilter"
                });

                ruleProperties = await GetAllRulesAsync(ruleManager);

                Assert.True(ruleProperties.Count() == 1);
                Assert.AreEqual("BooleanFilter", ruleProperties.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
        public async Task CorrelationRuleFilterOnTheMessageProperties()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());

                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesDescription.First().Name);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new CorrelationRuleFilter {
                        Subject = "red"
                    },
                    Name = "CorrelationMsgPropertyRule"
                });

                rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual("CorrelationMsgPropertyRule", rulesDescription.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.Where(c => c.Color == "red").ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
Пример #7
0
        public async Task SqlRuleFilterOnTheMessageProperties()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(1, rulesProperties.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesProperties.First().Name);

                await ruleManager.DeleteRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.CreateRuleAsync(new CreateRuleOptions
                {
                    Filter = new SqlRuleFilter("sys.Label = 'yellow'"),
                    Name   = "SqlMsgPropertyRule"
                });

                ;

                rulesProperties = await GetAllRulesAsync(ruleManager);

                Assert.AreEqual(1, rulesProperties.Count());
                Assert.AreEqual("SqlMsgPropertyRule", rulesProperties.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.Where(c => c.Color == "yellow").ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
        public async Task DefaultFilter()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesDescription.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
        public async Task SqlRuleFilterUsingOROperator()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());

                IEnumerable <RuleProperties> rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual(RuleProperties.DefaultRuleName, rulesDescription.First().Name);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.AddRuleAsync(new RuleProperties
                {
                    Filter = new SqlRuleFilter("Color = 'blue' or Quantity = 10"),
                    Name   = "SqlRuleUsingOperator"
                });

                rulesDescription = await ruleManager.GetRulesAsync();

                Assert.AreEqual(1, rulesDescription.Count());
                Assert.AreEqual("SqlRuleUsingOperator", rulesDescription.First().Name);

                await SendMessages(client, scope.TopicName);

                var expectedOrders = Orders.Where(c => c.Color == "blue" || c.Quantity == 10).ToList();
                await ReceiveAndAssertMessages(
                    client,
                    scope.TopicName,
                    scope.SubscriptionNames.First(),
                    expectedOrders);
            }
        }
        public async Task AddGetAndRemoveRules()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);

                ServiceBusRuleManager ruleManager = client.CreateRuleManager(scope.TopicName, scope.SubscriptionNames.First());
                var sqlRuleName         = "sqlRule";
                var correlationRuleName = "correlationRule";

                var rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(1, rules.Count());
                var firstRule = rules[0];
                Assert.AreEqual(RuleProperties.DefaultRuleName, firstRule.Name);
                Assert.Null(firstRule.Action);

                await ruleManager.AddRuleAsync(sqlRuleName, new SqlRuleFilter("price > 10"));

                var ruleDescription = new RuleProperties(correlationRuleName)
                {
                    Filter = new CorrelationRuleFilter
                    {
                        CorrelationId         = "correlationId",
                        Subject               = "label",
                        MessageId             = "messageId",
                        ApplicationProperties =
                        {
                            { "key1", "value1" }
                        },
                        ReplyTo          = "replyTo",
                        ReplyToSessionId = "replyToSessionId",
                        SessionId        = "sessionId",
                        To = "to"
                    },
                    Action = new SqlRuleAction("Set CorrelationId = 'newValue'")
                };
                await ruleManager.AddRuleAsync(ruleDescription);

                rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(3, rules.Count);

                var sqlRule = rules.FirstOrDefault(rule => rule.Name.Equals(sqlRuleName));
                Assert.NotNull(sqlRule);
                Assert.Null(sqlRule.Action);
                Assert.IsInstanceOf <SqlRuleFilter>(sqlRule.Filter);
                Assert.AreEqual("price > 10", ((SqlRuleFilter)sqlRule.Filter).SqlExpression);

                var correlationRule = rules.FirstOrDefault(rule => rule.Name.Equals(correlationRuleName));
                Assert.NotNull(correlationRule);
                Assert.IsInstanceOf <SqlRuleAction>(correlationRule.Action);
                var sqlRuleAction = correlationRule.Action as SqlRuleAction;
                Assert.NotNull(sqlRuleAction);
                Assert.AreEqual("Set CorrelationId = 'newValue'", sqlRuleAction.SqlExpression);
                Assert.IsInstanceOf <CorrelationRuleFilter>(correlationRule.Filter);
                var correlationRuleFilter = correlationRule.Filter as CorrelationRuleFilter;
                Assert.NotNull(correlationRuleFilter);
                Assert.AreEqual("correlationId", correlationRuleFilter.CorrelationId);
                Assert.AreEqual("label", correlationRuleFilter.Subject);
                Assert.AreEqual("messageId", correlationRuleFilter.MessageId);
                Assert.AreEqual("replyTo", correlationRuleFilter.ReplyTo);
                Assert.AreEqual("replyToSessionId", correlationRuleFilter.ReplyToSessionId);
                Assert.AreEqual("sessionId", correlationRuleFilter.SessionId);
                Assert.AreEqual("to", correlationRuleFilter.To);
                Assert.NotNull(correlationRuleFilter.ApplicationProperties);
                Assert.AreEqual("value1", correlationRuleFilter.ApplicationProperties["key1"]);

                await ruleManager.RemoveRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.RemoveRuleAsync(sqlRuleName);

                await ruleManager.RemoveRuleAsync(correlationRuleName);

                rules = (await ruleManager.GetRulesAsync()).ToList();
                Assert.AreEqual(0, rules.Count());
            }
        }
Пример #11
0
        public async Task ManageRules()
        {
            await using (var scope = await ServiceBusScope.CreateWithTopic(enablePartitioning: false, enableSession: false))
            {
                #region Snippet:ServiceBusManageRules
#if SNIPPET
                string connectionString = "<connection_string>";
                string topicName        = "<topic_name>";
                string subscriptionName = "<subscription_name>";
#else
                string connectionString = TestEnvironment.ServiceBusConnectionString;
                string topicName        = scope.TopicName;
                string subscriptionName = scope.SubscriptionNames.First();
#endif

                await using var client = new ServiceBusClient(connectionString);

                await using ServiceBusRuleManager ruleManager = client.CreateRuleManager(topicName, subscriptionName);

                // By default, subscriptions are created with a default rule that always evaluates to True. In order to filter, we need
                // to delete the default rule. You can skip this step if you create the subscription with the ServiceBusAdministrationClient,
                // and specify a the FalseRuleFilter in the create rule options.
                await ruleManager.DeleteRuleAsync(RuleProperties.DefaultRuleName);

                await ruleManager.CreateRuleAsync("brand-filter", new CorrelationRuleFilter { Subject = "Toyota" });

                // create the sender
                ServiceBusSender sender = client.CreateSender(topicName);

                ServiceBusMessage[] messages =
                {
                    new ServiceBusMessage {
                        Subject = "Ford", ApplicationProperties ={                         { "Price", 25000 } }
                    },
                    new ServiceBusMessage {
                        Subject = "Toyota", ApplicationProperties ={                         { "Price", 28000 } }
                    },
                    new ServiceBusMessage {
                        Subject = "Honda", ApplicationProperties ={                         { "Price", 35000 } }
                    }
                };

                // send the messages
                await sender.SendMessagesAsync(messages);

                // create a receiver for our subscription that we can use to receive and settle the message
                ServiceBusReceiver receiver = client.CreateReceiver(topicName, subscriptionName);

                // receive the message - we only get back the Toyota message
                while (true)
                {
                    ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(5));

                    if (receivedMessage == null)
                    {
                        break;
                    }
                    Console.WriteLine($"Brand: {receivedMessage.Subject}, Price: {receivedMessage.ApplicationProperties["Price"]}");
                    await receiver.CompleteMessageAsync(receivedMessage);
                }

                await ruleManager.CreateRuleAsync("price-filter", new SqlRuleFilter("Price < 30000"));

                await ruleManager.DeleteRuleAsync("brand-filter");

                // we can also use the rule manager to iterate over the rules on the subscription.
                await foreach (RuleProperties rule in ruleManager.GetRulesAsync())
                {
                    // we should only have 1 rule at this point - "price-filter"
                    Console.WriteLine(rule.Name);
                }

                // send the messages again - because the subscription rules are evaluated when the messages are first enqueued, adding rules
                // for messages that are already in a subscription would have no effect.
                await sender.SendMessagesAsync(messages);

                // receive the messages - we get back both the Ford and the Toyota
                while (true)
                {
                    ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(5));

                    if (receivedMessage == null)
                    {
                        break;
                    }
                    Console.WriteLine($"Brand: {receivedMessage.Subject}, Price: {receivedMessage.ApplicationProperties["Price"]}");
                }
                #endregion
            }
        }