Ejemplo n.º 1
0
        public async Task PeekUsingConnectionStringWithSisgnature()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var options          = new ServiceBusClientOptions();
                var audience         = ServiceBusConnection.BuildConnectionResource(options.TransportType, TestEnvironment.FullyQualifiedNamespace, scope.QueueName);
                var connectionString = TestEnvironment.BuildConnectionStringWithSharedAccessSignature(scope.QueueName, audience);

                await using var client = new ServiceBusClient(connectionString, options);
                var messageCt = 10;

                ServiceBusSender sender = client.CreateSender(scope.QueueName);
                using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync();

                IEnumerable <ServiceBusMessage> sentMessages = AddMessages(batch, messageCt).AsEnumerable <ServiceBusMessage>();

                await sender.SendMessagesAsync(batch);

                await using var receiver = client.CreateReceiver(scope.QueueName);
                var messageEnum = sentMessages.GetEnumerator();

                var ct = 0;
                while (ct < messageCt)
                {
                    foreach (ServiceBusReceivedMessage peekedMessage in await receiver.PeekMessagesAsync(
                                 maxMessages: messageCt))
                    {
                        messageEnum.MoveNext();
                        Assert.AreEqual(messageEnum.Current.MessageId, peekedMessage.MessageId);
                        ct++;
                    }
                }
                Assert.AreEqual(messageCt, ct);
            }
        }
        public async Task ClientCanConnectUsingSharedKeyCredential()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: true, enableSession: false))
            {
                var options    = new ServiceBusClientOptions();
                var audience   = ServiceBusConnection.BuildConnectionResource(options.TransportType, TestEnvironment.FullyQualifiedNamespace, scope.QueueName);
                var credential = new AzureNamedKeyCredential(TestEnvironment.SharedAccessKeyName, TestEnvironment.SharedAccessKey);

                await using (var client = new ServiceBusClient(TestEnvironment.FullyQualifiedNamespace, credential, options))
                {
                    Assert.That(async() =>
                    {
                        ServiceBusReceiver receiver = null;

                        try
                        {
                            receiver = client.CreateReceiver(scope.QueueName);
                        }
                        finally
                        {
                            await(receiver?.DisposeAsync() ?? new ValueTask());
                        }
                    }, Throws.Nothing);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task SendConnStringWithSignature()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var options          = new ServiceBusClientOptions();
                var audience         = ServiceBusConnection.BuildConnectionResource(options.TransportType, TestEnvironment.FullyQualifiedNamespace, scope.QueueName);
                var connectionString = TestEnvironment.BuildConnectionStringWithSharedAccessSignature(scope.QueueName, audience);

                await using var sender = new ServiceBusClient(connectionString, options).CreateSender(scope.QueueName);
                await sender.SendMessageAsync(GetMessage());
            }
        }