Exemplo n.º 1
0
        private static async Task SendTextMessage()
        {
            const string queueName = "sbq-text-message";

            var managementClient = new ServiceBusAdministrationClient(Config.Namespace, Config.Credential);

            if (!await managementClient.QueueExistsAsync(queueName))
            {
                await managementClient.CreateQueueAsync(queueName);
            }

            await using var client = new ServiceBusClient(Config.Namespace, Config.Credential);

            var sender = client.CreateSender(queueName);

            var message = new ServiceBusMessage(Encoding.UTF8.GetBytes("This is a simple test message"));

            Console.WriteLine("Press any key to send a message. Press Enter to exit.");

            while (Console.ReadKey(true).Key != ConsoleKey.Enter)
            {
                await sender.SendMessageAsync(message);

                Console.WriteLine($"Message Sent for {nameof(SendTextMessage)}");
            }

            Console.ReadLine();

            await managementClient.DeleteQueueAsync(queueName);
        }
Exemplo n.º 2
0
        public async Task GetUpdateDeleteQueue()
        {
            string queueName        = Guid.NewGuid().ToString("D").Substring(0, 8);
            string connectionString = TestEnvironment.ServiceBusConnectionString;
            var    client           = new ServiceBusAdministrationClient(connectionString);
            var    qd = new CreateQueueOptions(queueName);
            await client.CreateQueueAsync(qd);

            #region Snippet:GetQueue
            QueueProperties queue = await client.GetQueueAsync(queueName);

            #endregion
            #region Snippet:UpdateQueue
            queue.LockDuration = TimeSpan.FromSeconds(60);
            QueueProperties updatedQueue = await client.UpdateQueueAsync(queue);

            #endregion
            Assert.AreEqual(TimeSpan.FromSeconds(60), updatedQueue.LockDuration);
            #region Snippet:DeleteQueue
            await client.DeleteQueueAsync(queueName);

            #endregion
            Assert.That(
                async() =>
                await client.GetQueueAsync(queueName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusFailureReason.MessagingEntityNotFound));
        }
Exemplo n.º 3
0
        public static async Task Stage(string connectionString, string destination)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.QueueExistsAsync(destination))
            {
                await client.DeleteQueueAsync(destination);
            }
            await client.CreateQueueAsync(destination);
        }
Exemplo n.º 4
0
        public static async Task Stage(string connectionString)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            async Task DeleteIfExists(string queueName)
            {
                if (await client.QueueExistsAsync(queueName))
                {
                    await client.DeleteQueueAsync(queueName);
                }
            }

            await Task.WhenAll(
                DeleteIfExists("Hop4"),
                DeleteIfExists("Hop3"),
                DeleteIfExists("Hop2"),
                DeleteIfExists("Hop1"),
                DeleteIfExists("Hop0"),
                DeleteIfExists("Hop")
                );

            var description = new CreateQueueOptions("Hop");
            await client.CreateQueueAsync(description);

            description = new CreateQueueOptions("Hop0");
            await client.CreateQueueAsync(description);

            description = new CreateQueueOptions("Hop1")
            {
                ForwardTo = "Hop0"
            };
            await client.CreateQueueAsync(description);

            description = new CreateQueueOptions("Hop2")
            {
                ForwardTo = "Hop1"
            };
            await client.CreateQueueAsync(description);

            description = new CreateQueueOptions("Hop3")
            {
                ForwardTo = "Hop2"
            };
            await client.CreateQueueAsync(description);

            description = new CreateQueueOptions("Hop4")
            {
                ForwardTo = "Hop3"
            };
            await client.CreateQueueAsync(description);
        }
        public static async Task Stage(string connectionString, string destination)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.QueueExistsAsync(destination))
            {
                await client.DeleteQueueAsync(destination);
            }

            var description = new CreateQueueOptions(destination)
            {
                MaxDeliveryCount = int.MaxValue
            };
            await client.CreateQueueAsync(description);
        }
        public static async Task Stage(string connectionString, string destination)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.QueueExistsAsync(destination))
            {
                await client.DeleteQueueAsync(destination);
            }

            var queueDescription = new CreateQueueOptions(destination)
            {
                RequiresSession = true
            };
            await client.CreateQueueAsync(queueDescription);
        }
Exemplo n.º 7
0
        public static async Task Stage(string connectionString, string destination)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.QueueExistsAsync(destination))
            {
                await client.DeleteQueueAsync(destination);
            }

            var description = new CreateQueueOptions(destination)
            {
                DeadLetteringOnMessageExpiration = true, // default false
                MaxDeliveryCount = 1
            };
            await client.CreateQueueAsync(description);
        }
Exemplo n.º 8
0
        public static async Task Stage(string connectionString, string destination)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.QueueExistsAsync(destination))
            {
                await client.DeleteQueueAsync(destination);
            }

            var queueDescription = new CreateQueueOptions(destination)
            {
                RequiresDuplicateDetection          = true,
                DuplicateDetectionHistoryTimeWindow = TimeSpan.FromSeconds(20)
            };
            await client.CreateQueueAsync(queueDescription);
        }
Exemplo n.º 9
0
        private static async Task SendComplexObjectMessage()
        {
            const string queueName = "sbq-complex-object-message";

            var managementClient = new ServiceBusAdministrationClient(Config.Namespace, Config.Credential);

            if (!await managementClient.QueueExistsAsync(queueName))
            {
                await managementClient.CreateQueueAsync(queueName);
            }

            await using var client = new ServiceBusClient(Config.Namespace, Config.Credential);

            var sender = client.CreateSender(queueName);

            var payment = new Payment
            {
                PaymentId     = Guid.NewGuid(),
                AccountNumber = "132456789",
                Amount        = 1337m,
                PaymentDate   = DateTime.Today.AddDays(1),
                Payee         = "Mr John Smith"
            };

            var message = new ServiceBusMessage(JsonSerializer.SerializeToUtf8Bytes(payment));

            Console.WriteLine("Press any key to send a message. Press Enter to exit.");

            while (Console.ReadKey(true).Key != ConsoleKey.Enter)
            {
                await sender.SendMessageAsync(message);

                Console.WriteLine($"Message Sent for {nameof(SendComplexObjectMessage)}");
            }

            Console.ReadLine();

            await managementClient.DeleteQueueAsync(queueName);
        }
Exemplo n.º 10
0
        private static async Task SendTextMessageWithProperties()
        {
            const string queueName = "sbq-text-message-with-properties";

            var managementClient = new ServiceBusAdministrationClient(Config.Namespace, Config.Credential);

            if (!await managementClient.QueueExistsAsync(queueName))
            {
                await managementClient.CreateQueueAsync(queueName);
            }

            await using var client = new ServiceBusClient(Config.Namespace, Config.Credential);

            var sender = client.CreateSender(queueName);

            var message = new ServiceBusMessage
            {
                Body                  = new BinaryData("This is a simple test message"),
                ContentType           = "text/plain",
                CorrelationId         = Guid.NewGuid().ToString(),
                MessageId             = Guid.NewGuid().ToString(),
                TimeToLive            = TimeSpan.FromMinutes(10),
                ScheduledEnqueueTime  = DateTime.UtcNow,
                ApplicationProperties = { { "custom-property", "Custom Value" } }
            };

            Console.WriteLine("Press any key to send a message. Press Enter to exit.");

            while (Console.ReadKey(true).Key != ConsoleKey.Enter)
            {
                await sender.SendMessageAsync(message);

                Console.WriteLine($"Message Sent for {nameof(SendTextMessageWithProperties)}");
            }

            Console.ReadLine();

            await managementClient.DeleteQueueAsync(queueName);
        }
Exemplo n.º 11
0
        public async Task <T> Request <T>(string queueName, object payload) where T : class
        {
            var temporaryQueueName = Guid.NewGuid().ToString();

            var timeoutMillis            = Math.Max(MinimumAutoDeleteOnIdleMillis, 2 * _options.RequestTimeOutMillis);
            var autoDeleteOnIdleTimespan = TimeSpan.FromMilliseconds(timeoutMillis);

            var createQueueOptions = new CreateQueueOptions(temporaryQueueName)
            {
                AutoDeleteOnIdle = autoDeleteOnIdleTimespan,
                Name             = temporaryQueueName
            };

            await _administrationClient.CreateQueueAsync(createQueueOptions);

            var sender   = _clientFactory.CreateSendClient(queueName);
            var receiver = _clientFactory.CreateReceiverClient(temporaryQueueName);

            try
            {
                var outboundMessage = new ServiceBusMessage(JsonSerializer.SerializeToUtf8Bytes(payload, Constants.DefaultJsonSerializerOptions))
                {
                    ReplyTo = temporaryQueueName
                };
                await sender.SendMessageAsync(outboundMessage);

                var reply = await receiver.ReceiveMessageAsync(_options.RequestTimeout);

                return(reply != null
                    ? JsonSerializer.Deserialize <T>(reply.Body, Constants.DefaultJsonSerializerOptions)
                    : null);
            }
            finally
            {
                await _administrationClient.DeleteQueueAsync(temporaryQueueName)
                .ConfigureAwait(false);
            }
        }
Exemplo n.º 12
0
 static async Task LeaveStage(string connectionString, string destination)
 {
     var client = new ServiceBusAdministrationClient(connectionString);
     await client.DeleteQueueAsync(destination);
 }
Exemplo n.º 13
0
 public async ValueTask DisposeAsync() =>
 await _adminClient.DeleteQueueAsync(QueueName).ConfigureAwait(false);
Exemplo n.º 14
0
        private static async Task SendComplexObjectMessageWithDuplicateDetection()
        {
            const string queueName = "sbq-complex-object-message-with-duplicate";

            var managementClient = new ServiceBusAdministrationClient(Config.Namespace, Config.Credential);

            var createQueueOptions = new CreateQueueOptions(queueName)
            {
                RequiresDuplicateDetection          = true,
                DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(10)
            };

            if (!await managementClient.QueueExistsAsync(queueName))
            {
                await managementClient.CreateQueueAsync(createQueueOptions);
            }

            await using var client = new ServiceBusClient(Config.Namespace, Config.Credential);

            var sender = client.CreateSender(queueName);

            var payments = new List <Payment>
            {
                new Payment
                {
                    PaymentId     = Guid.NewGuid(),
                    AccountNumber = "132456789",
                    Amount        = 1337m,
                    PaymentDate   = DateTime.Today.AddDays(1),
                    Payee         = "Mr John Smith"
                },
                new Payment
                {
                    PaymentId     = Guid.NewGuid(),
                    AccountNumber = "1576321357",
                    Amount        = 6984.56m,
                    PaymentDate   = DateTime.Today.AddDays(3),
                    Payee         = "Mrs Jane Doe"
                },
                new Payment
                {
                    PaymentId     = Guid.NewGuid(),
                    AccountNumber = "1867817635",
                    Amount        = 13872m,
                    PaymentDate   = DateTime.Today,
                    Payee         = "Mr Robert Smith"
                },
                new Payment
                {
                    PaymentId     = Guid.NewGuid(),
                    AccountNumber = "1779584565",
                    Amount        = 20000m,
                    PaymentDate   = DateTime.Today.AddDays(9),
                    Payee         = "Mrs James Doe"
                },
                new Payment
                {
                    PaymentId     = Guid.NewGuid(),
                    AccountNumber = "1657892587",
                    Amount        = 900000m,
                    PaymentDate   = DateTime.Today,
                    Payee         = "Mr William Tell"
                }
            };

            Console.WriteLine("Press any key to send all payment messages. Press Enter to exit.");

            while (Console.ReadKey(true).Key != ConsoleKey.Enter)
            {
                Console.WriteLine($"Total Payments to send: {payments.Count}");

                foreach (var payment in payments)
                {
                    var message = new ServiceBusMessage(JsonSerializer.SerializeToUtf8Bytes(payment))
                    {
                        MessageId = payment.PaymentId.ToString() // Needed to detect duplicate messages
                    };

                    var random = new Random();

                    if (random.NextDouble() > 0.4) // Randomly simulate sending duplicate messages
                    {
                        await sender.SendMessageAsync(message);

                        Console.WriteLine($"Message Sent for {nameof(SendComplexObjectMessageWithDuplicateDetection)} - Payment ID: {payment.PaymentId}");
                    }
                    else
                    {
                        await sender.SendMessageAsync(message);

                        Console.WriteLine($"Message Sent for {nameof(SendComplexObjectMessageWithDuplicateDetection)} - Payment ID: {payment.PaymentId}");

                        await sender.SendMessageAsync(message);

                        Console.WriteLine($"Message Sent for {nameof(SendComplexObjectMessageWithDuplicateDetection)} - Payment ID: {payment.PaymentId}");
                    }
                }
            }

            Console.ReadLine();

            await managementClient.DeleteQueueAsync(queueName);
        }
Exemplo n.º 15
0
        private void DeleteServiceBusQueue(string serviceBusQueueName)
        {
            var busAdmin = new ServiceBusAdministrationClient(Connections.ServiceBusConnectionString);

            busAdmin.DeleteQueueAsync(serviceBusQueueName).Wait();
        }
Exemplo n.º 16
0
 public static Task Delete(ServiceBusAdministrationClient client, CommandArgument name)
 {
     return(client.DeleteQueueAsync(name.Value));
 }
Exemplo n.º 17
0
        public static async Task <Azure.Response> DeleteQueueAsync(string connectionString, string queueName)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            return(await client.DeleteQueueAsync(queueName));
        }