Exemplo n.º 1
0
        //private static string _subscriptionName = "demosubscription2";

        static async Task Main(string[] args)
        {
            var connStr   = "";
            var topicName = "demotopic1";

            _topicClient = new ServiceBusClient(connStr);
            _processor   = _topicClient.CreateProcessor(topicName, _subscriptionName, new ServiceBusProcessorOptions());

            try
            {
                _processor.ProcessMessageAsync += MessageHandler;
                _processor.ProcessErrorAsync   += ErrorHandler;

                await _processor.StartProcessingAsync();

                Console.WriteLine("Wait for a minute and then press any key to end the processing");
                Console.ReadKey();

                Console.WriteLine("Stopping");

                await _processor.StopProcessingAsync();

                Console.WriteLine("Stopped");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            finally
            {
                await _processor.DisposeAsync();

                await _topicClient.DisposeAsync();
            }
        }
Exemplo n.º 2
0
        public async Task CanDisposeStartedProcessorMultipleTimes()
        {
            var processor = new ServiceBusProcessor(
                GetMockedReceiverConnection(),
                "entityPath",
                false,
                new ServiceBusProcessorOptions());

            processor.ProcessMessageAsync += _ => Task.CompletedTask;
            processor.ProcessErrorAsync   += _ => Task.CompletedTask;
            await processor.StartProcessingAsync().ConfigureAwait(false);

            await processor.DisposeAsync();

            await processor.DisposeAsync();
        }
Exemplo n.º 3
0
        public async Task CannotStartProcessorWhenConnectionIsClosed()
        {
            var connectionClosed    = false;
            var mockTransportClient = new Mock <TransportClient>();
            var mockConnection      = new Mock <ServiceBusConnection>("not.real.com", Mock.Of <TokenCredential>(), new ServiceBusClientOptions())
            {
                CallBase = true
            };

            mockTransportClient
            .SetupGet(client => client.IsClosed)
            .Returns(() => connectionClosed);

            mockConnection
            .Setup(connection => connection.CreateTransportClient(
                       It.IsAny <ServiceBusTokenCredential>(),
                       It.IsAny <ServiceBusClientOptions>()))
            .Returns(mockTransportClient.Object);

            var processor = new ServiceBusProcessor(
                mockConnection.Object,
                "entityPath",
                false,
                new ServiceBusProcessorOptions());

            processor.ProcessMessageAsync += _ => Task.CompletedTask;
            processor.ProcessErrorAsync   += _ => Task.CompletedTask;

            connectionClosed = true;

            Assert.That(async() => await processor.StartProcessingAsync(),
                        Throws.InstanceOf <ObjectDisposedException>().And.Property(nameof(ObjectDisposedException.ObjectName)).EqualTo(nameof(ServiceBusConnection)));

            await processor.DisposeAsync();
        }
Exemplo n.º 4
0
 public void Dispose()
 {
     _processor.DisposeAsync().GetAwaiter().GetResult();
     _processor = null;
     _client.DisposeAsync().GetAwaiter().GetResult();
     _client = null;
 }
        static async Task Main()
        {
            client    = new ServiceBusClient(connectionString);
            processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());

            try
            {
                processor.ProcessMessageAsync += MessageHandler;
                processor.ProcessErrorAsync   += ErrorHandler;

                await processor.StartProcessingAsync();

                Console.WriteLine("Wait for a minute and then press any key to end the processing");
                Console.ReadKey();

                Console.WriteLine("\nStopping the receiver...");
                await processor.StopProcessingAsync();

                Console.WriteLine("Stopped receiving messages");
            }
            finally
            {
                await processor.DisposeAsync();

                await client.DisposeAsync();
            }
        }
Exemplo n.º 6
0
        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            await processor.DisposeAsync();

            await serviceBusClient.DisposeAsync();

            await base.StopAsync(cancellationToken);
        }
Exemplo n.º 7
0
        public async Task Stop()
        {
            await checkOutProcessor.StopProcessingAsync();

            await checkOutProcessor.DisposeAsync();

            await orderUpdatePaymentStatusProcessor.StopProcessingAsync();

            await orderUpdatePaymentStatusProcessor.DisposeAsync();
        }
Exemplo n.º 8
0
        public async void Dispose()
        {
            // stop processing
            await processor.StopProcessingAsync();

            await processor.DisposeAsync();

            await sender.DisposeAsync();

            await serviceBusClient.DisposeAsync();
        }
Exemplo n.º 9
0
            public async Task Stop(bool initializedByAzureServiceBus)
            {
                _logger.Info($"ServiceBusProcessor for queue {_queueName} is stoping...");
                if (initializedByAzureServiceBus)
                {
                    _stop();
                }

                await _processor.StopProcessingAsync();

                await _processor.DisposeAsync();
            }
        /// <summary>
        /// Unsubscribe from messagebus and closes connection.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task UnSubscribeFromMessageBusAsync(CancellationToken cancellationToken = default)
        {
            if (_serviceBusProcessor is not null)
            {
                await _serviceBusProcessor.DisposeAsync();
            }

            if (_serviceBusClient is not null)
            {
                await _serviceBusClient.DisposeAsync();
            }
        }
Exemplo n.º 11
0
        public async ValueTask DisposeAsync()
        {
            // calling DisposeAsync() on each processor might take a long time to complete (~60sec) due to
            // apparent limitations of the underlying AMQP library.
            // more details here: https://github.com/Azure/azure-sdk-for-net/issues/19306
            // Therefore we do it only when in Release mode. Debug mode is used when running the tests suite.
            await _processor.StartProcessingAsync();

            _processor.ProcessMessageAsync -= MessageHandler;
            _processor.ProcessErrorAsync   -= ProcessErrorAsync;
            await _processor.DisposeAsync();

            _processor = null;
        }
Exemplo n.º 12
0
        public async Task CannotStartProcessorWhenProcessorIsDisposed()
        {
            var mockConnection = GetMockedReceiverConnection();

            var processor = new ServiceBusProcessor(
                mockConnection,
                "entityPath",
                false,
                new ServiceBusProcessorOptions());

            processor.ProcessMessageAsync += _ => Task.CompletedTask;
            processor.ProcessErrorAsync   += _ => Task.CompletedTask;

            await processor.DisposeAsync();

            Assert.That(async() => await processor.StartProcessingAsync(),
                        Throws.InstanceOf <ObjectDisposedException>().And.Property(nameof(ObjectDisposedException.ObjectName)).EqualTo(nameof(ServiceBusProcessor)));
        }
Exemplo n.º 13
0
        static async Task Main()
        {
            // The Service Bus client types are safe to cache and use as a singleton for the lifetime
            // of the application, which is best practice when messages are being published or read
            // regularly.
            //

            // Create the client object that will be used to create sender and receiver objects
            client = new ServiceBusClient(connectionString);

            // create a processor that we can use to process the messages
            processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions());

            try
            {
                // add handler to process messages
                processor.ProcessMessageAsync += MessageHandler;

                // add handler to process any errors
                processor.ProcessErrorAsync += ErrorHandler;

                // start processing
                await processor.StartProcessingAsync();

                Console.WriteLine("Wait for a minute and then press any key to end the processing");
                Console.ReadKey();

                // stop processing
                Console.WriteLine("\nStopping the receiver...");
                await processor.StopProcessingAsync();

                Console.WriteLine("Stopped receiving messages");
            }
            finally
            {
                // Calling DisposeAsync on client types is required to ensure that network
                // resources and other unmanaged objects are properly cleaned up.
                await processor.DisposeAsync();

                await client.DisposeAsync();
            }
        }
Exemplo n.º 14
0
        public async ValueTask DisposeAsync()
        {
            await _serviceBusProcessor.DisposeAsync();

            await _serviceBusSender.DisposeAsync();
        }
Exemplo n.º 15
0
 public ValueTask DisposeAsync()
 {
     return(_processor?.DisposeAsync() ?? ValueTask.CompletedTask);
 }
Exemplo n.º 16
0
 public async ValueTask DisposeAsync() => await _processor.DisposeAsync();
 protected virtual void Dispose(bool disposing)
 {
     serviceBusClient?.DisposeAsync().GetAwaiter().GetResult();
     processor?.DisposeAsync().GetAwaiter().GetResult();
 }
Exemplo n.º 18
0
        public async Task Stop()
        {
            await orderPaymentProcessor.StopProcessingAsync();

            await orderPaymentProcessor.DisposeAsync();
        }