Esempio n. 1
0
        public async Task ProcessQueue()
        {
            Debug.WriteLine("Cleaning the subscription...");
            await _queueCleanUp;

            var connection = new ServiceBusConnection(ConnectionString);
            var queue      = new QueueClient(connection, QueueName, ReceiveMode.PeekLock, RetryPolicy.Default);

            await Observable
            .Range(0, 3)
            .Select(i => Encoding.UTF8.GetBytes($"Message {i}"))
            .SelectMany(body => queue.SendAsync(new Message(body)).ToObservable());

            var received = await connection
                           .ProcessMessages((message, ct) =>
            {
                var text = Encoding.UTF8.GetString(message.Body);
                return(Task.FromResult(text));
            })
                           .FromQueue(QueueName, ReceiveMode.PeekLock, RetryPolicy.Default)
                           .Take(TimeSpan.FromSeconds(1))
                           .ToList();


            Assert.Equal(3, received.Count);
            await Observable
            .Range(0, 3)
            .Do(i => Assert.Contains($"Message {i}", received));
        }
Esempio n. 2
0
        public ServiceBusConnection_ProcessMessages_Should()
        {
            ConnectionString = Configurations.Instance.ConnectionString;
            var connection = new ServiceBusConnection(ConnectionString);


            _subscriptionCleanUp = connection
                                   .ProcessMessages((m, ct) => Task.CompletedTask)
                                   .FromSubscription(TopicName, SubscriptionName, ReceiveMode.PeekLock, RetryPolicy.Default)
                                   .Take(TimeSpan.FromMilliseconds(300))
                                   .Count()
                                   .Do(count => Debug.WriteLine($"Deleted {count} messages"))
                                   .LastOrDefaultAsync();

            _queueCleanUp = connection
                            .ProcessMessages((m, ct) => Task.CompletedTask)
                            .FromQueue(QueueName, ReceiveMode.PeekLock, RetryPolicy.Default)
                            .Take(TimeSpan.FromMilliseconds(300))
                            .Count()
                            .Do(count => Debug.WriteLine($"Deleted {count} messages"))
                            .LastOrDefaultAsync();
        }
Esempio n. 3
0
        static async Task <int> Main(string[] args)
        {
            var connectionString = args?.FirstOrDefault() ?? Environment.GetEnvironmentVariable("ConnectionString");

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                Console.WriteLine("Connection string is required");
                return(1);
            }

            await ProvisionSamplesAsync(connectionString);

            var connection = new ServiceBusConnection(connectionString);

            var topicClient = new TopicClient(connection, TopicName, RetryPolicy.Default);
            var queueClient = new QueueClient(connection, QueueName, ReceiveMode.PeekLock, RetryPolicy.Default);



            Console.WriteLine("---  Topic/Subscription sample -------------");

            await topicClient
            .SendAsync(new Message(Encoding.UTF8.GetBytes($"This message has been sent to {TopicName}")));

            await topicClient
            .ProcessMessages(async (message, token) =>
            {
                var text = Encoding.UTF8.GetString(message.Body);
                Console.WriteLine(text);
            })
            .FromSubscription(SubscriptionName, ReceiveMode.PeekLock)
            .FirstOrDefaultAsync();

            await Observable.Range(0, 3).Do(_ => Console.WriteLine());


            Console.WriteLine("---  Queue sample  --------------------------");
            await queueClient
            .SendAsync(new Message(Encoding.UTF8.GetBytes($"This message has been sent to {QueueName}")));

            await connection
            .ProcessMessages(async (message, token) =>
            {
                var text = Encoding.UTF8.GetString(message.Body);
                Console.WriteLine(text);
            })
            .FromQueue(QueueName, ReceiveMode.PeekLock, RetryPolicy.Default)
            .FirstOrDefaultAsync();

            return(0);
        }