public async Task BeginListening(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                try
                {
                    var(msg, msgId, transaction) =
                        await GetMessage(
                            _connectionString,
                            _queueName,
                            token);

                    OnMessageReceived?.Invoke(
                        this,
                        new MessageEventArgs
                    {
                        Message = new AcknowledgeableMessage(
                            msg,
                            msgId,
                            transaction)
                    });
                }
                catch (Exception e)
                {
                    OnMessageException?.Invoke(
                        this,
                        new MessageExceptionArgs
                    {
                        Exception = e
                    });

                    await Task.Delay(1000, token);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Set a custom error handler.
        /// </summary>
        /// <param name="handler"></param>
        /// <returns></returns>
        public TConfiguration WithErrorHandler(OnMessageException handler)
        {
            ErrorHandler = handler;

            // Continue.
            return(this as TConfiguration);
        }
Example #3
0
        public Task StartAsync(OnMessage messageHandler, OnMessageException exceptionHandler, OnMessageOptions options)
        {
            lock (_initializationLock)
            {
                if (_initialized)
                {
                    throw new MessageReceiverException("Message receiver has already been initialized.");
                }

                if (messageHandler == null)
                {
                    throw new ArgumentNullException("messageHandler");
                }

                if (options == null)
                {
                    options = _defaultOptions;
                }

                // Log.
                ServiceBusEventSource.Log.MessagePumpStart(GetType().Name, Namespace, Path, options.AutoRenewTimeout, options.MaxConcurrentCalls);

                // Initialize the handler options.
                var messageOptions = new Microsoft.ServiceBus.Messaging.OnMessageOptions();
                messageOptions.AutoComplete       = options.AutoComplete;
                messageOptions.AutoRenewTimeout   = options.AutoRenewTimeout;
                messageOptions.MaxConcurrentCalls = options.MaxConcurrentCalls;
                messageOptions.ExceptionReceived += (s, e) =>
                {
                    if (e.Exception != null)
                    {
                        // Log.
                        ServiceBusEventSource.Log.MessagePumpExceptionReceived(Namespace, Path,
                                                                               e.Action, e.Exception);

                        // Handle exception.
                        if (exceptionHandler != null)
                        {
                            exceptionHandler(e.Action, e.Exception);
                        }
                    }
                };

                // Mark receiver as initialized.
                _initialized = true;

                // Start.
                return(OnStartAsync(messageHandler, messageOptions));
            }
        }
Example #4
0
        public Task StartAsync(OnMessage messageHandler, OnMessageException exceptionHandler, OnMessageOptions options)
        {
            lock (_initializationLock)
            {
                if (_initialized)
                    throw new MessageReceiverException("Message receiver has already been initialized.");

                if (messageHandler == null)
                    throw new ArgumentNullException("messageHandler");

                if (options == null)
                    options = _defaultOptions;

                // Log.
                ServiceBusEventSource.Log.MessagePumpStart(GetType().Name, Namespace, Path, options.AutoRenewTimeout, options.MaxConcurrentCalls);

                // Initialize the handler options.
                var messageOptions = new Microsoft.ServiceBus.Messaging.OnMessageOptions();
                messageOptions.AutoComplete = options.AutoComplete;
                messageOptions.AutoRenewTimeout = options.AutoRenewTimeout;
                messageOptions.MaxConcurrentCalls = options.MaxConcurrentCalls;
                messageOptions.ExceptionReceived += (s, e) => 
                {
                    if (e.Exception != null)
                    {
                        // Log.
                        ServiceBusEventSource.Log.MessagePumpExceptionReceived(Namespace, Path,
                            e.Action, e.Exception);

                        // Handle exception.
                        if (exceptionHandler != null)
                            exceptionHandler(e.Action, e.Exception);
                    }
                };

                // Mark receiver as initialized.
                _initialized = true;

                // Start.
                return OnStartAsync(messageHandler, messageOptions);
            }
        }
 public static Task StartAsync(this IMessagePump pump, OnMessage messageHandler, OnMessageException exception)
 {
     return pump.StartAsync(messageHandler, exception, null);
 }
 public static Task StartAsync(this IMessagePump pump, OnMessage messageHandler, OnMessageException exception)
 {
     return(pump.StartAsync(messageHandler, exception, null));
 }