コード例 #1
0
        protected virtual async Task HandleIncomingMessageAsync(object sender, BasicDeliverEventArgs basicDeliverEventArgs)
        {
            try
            {
                foreach (var callback in Callbacks)
                {
                    await callback(Channel, basicDeliverEventArgs);
                }

                Channel.BasicAck(basicDeliverEventArgs.DeliveryTag, multiple: false);
            }
            catch (Exception ex)
            {
                try
                {
                    Channel.BasicNack(
                        basicDeliverEventArgs.DeliveryTag,
                        multiple: false,
                        requeue: true
                        );
                }
                catch { }

                Logger.LogException(ex);
                await ExceptionNotifier.NotifyAsync(ex);
            }
        }
コード例 #2
0
    protected virtual void Consume()
    {
        Consumer = ConsumerPool.Get(GroupId, ConnectionName);

        Task.Factory.StartNew(async() =>
        {
            Consumer.Subscribe(TopicName);

            while (true)
            {
                try
                {
                    var consumeResult = Consumer.Consume();

                    if (consumeResult.IsPartitionEOF)
                    {
                        continue;
                    }

                    await HandleIncomingMessage(consumeResult);
                }
                catch (ConsumeException ex)
                {
                    Logger.LogException(ex, LogLevel.Warning);
                    await ExceptionNotifier.NotifyAsync(ex, logLevel: LogLevel.Warning);
                }
            }
        }, TaskCreationOptions.LongRunning);
    }
コード例 #3
0
            public void ShouldThrowCodeContractViolationException()
            {
                //Arrange
                var notifier = new ExceptionNotifier();

                //Act and assert
                Assert.Throws<CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData()));
            }
コード例 #4
0
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            ExceptionNotifier.NotifyException(exception, context.Request);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;

            return(context.Response.WriteAsync(""));
        }
コード例 #5
0
            public void ShouldThrowCodeContractViolationException()
            {
                //Arrange
                var notifier = new ExceptionNotifier();


                //Act and assert
                Assert.Throws <CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData()));
            }
コード例 #6
0
            public void ThrownExceptionShouldHaveMessageFromData()
            {
                //Arrange
                const string expectedErrorMessage = "ExpectedErrorMessage";
                var notifier = new ExceptionNotifier();

                //Act
                var exception = Assert.Throws<CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(errorMessage: expectedErrorMessage)));

                //Assert
                Assert.Equal(expectedErrorMessage, exception.Message);
            }
コード例 #7
0
            public void ThrownExceptionShouldHaveValueFromData()
            {
                //Arrange
                const int expectedValue = 123456;
                var notifier = new ExceptionNotifier();

                //Act
                var exception = Assert.Throws<CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(expectedValue)));

                //Assert
                Assert.Equal(expectedValue, exception.Value);
            }
コード例 #8
0
            public void ThrownExceptionShouldHaveNameFromData()
            {
                //Arrange
                const string expectedName = "VariableName";
                var notifier = new ExceptionNotifier();

                //Act
                var exception = Assert.Throws<CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(name: expectedName)));

                //Assert
                Assert.Equal(expectedName, exception.Name);
            }
コード例 #9
0
            public void ThrownExceptionShouldHaveMessageFromData()
            {
                //Arrange
                const string expectedErrorMessage = "ExpectedErrorMessage";
                var          notifier             = new ExceptionNotifier();


                //Act
                var exception = Assert.Throws <CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(errorMessage: expectedErrorMessage)));


                //Assert
                Assert.Equal(expectedErrorMessage, exception.Message);
            }
コード例 #10
0
        protected virtual void DisposeChannel()
        {
            if (Channel == null)
            {
                return;
            }

            try {
                Channel.Dispose();
            } catch (Exception ex) {
                Logger.LogException(ex, LogLevel.Warning);
                AsyncHelper.RunSync(() => ExceptionNotifier.NotifyAsync(ex, logLevel: LogLevel.Warning));
            }
        }
コード例 #11
0
        protected virtual async Task HandleIncomingMessage(IModel channel, BasicDeliverEventArgs basicDeliverEventArgs)
        {
            try {
                foreach (var callback in Callbacks)
                {
                    await callback(channel, basicDeliverEventArgs);
                }

                channel.BasicAck(basicDeliverEventArgs.DeliveryTag, multiple: false);
            } catch (Exception ex) {
                Logger.LogException(ex);
                await ExceptionNotifier.NotifyAsync(ex);
            }
        }
コード例 #12
0
            public void ThrownExceptionShouldHaveValueFromData()
            {
                //Arrange
                const int expectedValue = 123456;
                var       notifier      = new ExceptionNotifier();


                //Act
                var exception = Assert.Throws <CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(expectedValue)));


                //Assert
                Assert.Equal(expectedValue, exception.Value);
            }
コード例 #13
0
            public void ThrownExceptionShouldHaveNameFromData()
            {
                //Arrange
                const string expectedName = "VariableName";
                var          notifier     = new ExceptionNotifier();


                //Act
                var exception = Assert.Throws <CodeContractViolationException>(() => notifier.Notify(TestDataFactory.CreateViolationData(name: expectedName)));


                //Assert
                Assert.Equal(expectedName, exception.Name);
            }
コード例 #14
0
    protected virtual async Task TryCreateChannelAsync()
    {
        await DisposeChannelAsync();

        try
        {
            Channel = ConnectionPool
                      .Get(ConnectionName)
                      .CreateModel();

            Channel.ExchangeDeclare(
                exchange: Exchange.ExchangeName,
                type: Exchange.Type,
                durable: Exchange.Durable,
                autoDelete: Exchange.AutoDelete,
                arguments: Exchange.Arguments
                );

            Channel.QueueDeclare(
                queue: Queue.QueueName,
                durable: Queue.Durable,
                exclusive: Queue.Exclusive,
                autoDelete: Queue.AutoDelete,
                arguments: Queue.Arguments
                );

            var consumer = new AsyncEventingBasicConsumer(Channel);
            consumer.Received += HandleIncomingMessageAsync;

            Channel.BasicConsume(
                queue: Queue.QueueName,
                autoAck: false,
                consumer: consumer
                );
        }
        catch (Exception ex)
        {
            if (ex is OperationInterruptedException operationInterruptedException &&
                operationInterruptedException.ShutdownReason.ReplyCode == 406 &&
                operationInterruptedException.Message.Contains("arg 'x-dead-letter-exchange'"))
            {
                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }

            Logger.LogException(ex, LogLevel.Warning);
            await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
        }
    }
コード例 #15
0
        protected virtual async Task TrySendQueueBindCommandsAsync()
        {
            try
            {
                while (!QueueBindCommands.IsEmpty)
                {
                    if (Channel == null || Channel.IsClosed)
                    {
                        return;
                    }

                    lock (ChannelSendSyncLock)
                    {
                        if (QueueBindCommands.TryPeek(out var command))
                        {
                            switch (command.Type)
                            {
                            case QueueBindType.Bind:
                                Channel.QueueBind(
                                    queue: Queue.QueueName,
                                    exchange: Exchange.ExchangeName,
                                    routingKey: command.RoutingKey
                                    );
                                break;

                            case QueueBindType.Unbind:
                                Channel.QueueUnbind(
                                    queue: Queue.QueueName,
                                    exchange: Exchange.ExchangeName,
                                    routingKey: command.RoutingKey
                                    );
                                break;

                            default:
                                throw new AbpException($"Unknown {nameof(QueueBindType)}: {command.Type}");
                            }

                            QueueBindCommands.TryDequeue(out command);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }
        }
コード例 #16
0
        protected virtual async Task DisposeChannelAsync()
        {
            if (Channel == null)
            {
                return;
            }

            try
            {
                Channel.Dispose();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }
        }
コード例 #17
0
        protected virtual async Task HandleIncomingMessage(ConsumeResult <string, byte[]> consumeResult)
        {
            try
            {
                foreach (var callback in Callbacks)
                {
                    await callback(consumeResult.Message);
                }

                Consumer.Commit(consumeResult);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                await ExceptionNotifier.NotifyAsync(ex);
            }
        }
コード例 #18
0
        protected virtual void TryCreateChannel()
        {
            DisposeChannel();

            try
            {
                var channel = ConnectionPool
                              .Get(ConnectionName)
                              .CreateModel();

                channel.ExchangeDeclare(
                    exchange: Exchange.ExchangeName,
                    type: Exchange.Type,
                    durable: Exchange.Durable,
                    autoDelete: Exchange.AutoDelete,
                    arguments: Exchange.Arguments
                    );

                channel.QueueDeclare(
                    queue: Queue.QueueName,
                    durable: Queue.Durable,
                    exclusive: Queue.Exclusive,
                    autoDelete: Queue.AutoDelete,
                    arguments: Queue.Arguments
                    );

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += async(model, basicDeliverEventArgs) =>
                {
                    await HandleIncomingMessage(channel, basicDeliverEventArgs);
                };

                channel.BasicConsume(
                    queue: Queue.QueueName,
                    autoAck: false,
                    consumer: consumer
                    );

                Channel = channel;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogLevel.Warning);
                AsyncHelper.RunSync(() => ExceptionNotifier.NotifyAsync(ex, logLevel: LogLevel.Warning));
            }
        }
コード例 #19
0
    protected virtual async Task TryCreateChannelAsync()
    {
        await DisposeChannelAsync();

        try
        {
            Channel = ConnectionPool
                      .Get(ConnectionName)
                      .CreateModel();

            Channel.ExchangeDeclare(
                exchange: Exchange.ExchangeName,
                type: Exchange.Type,
                durable: Exchange.Durable,
                autoDelete: Exchange.AutoDelete,
                arguments: Exchange.Arguments
                );

            Channel.QueueDeclare(
                queue: Queue.QueueName,
                durable: Queue.Durable,
                exclusive: Queue.Exclusive,
                autoDelete: Queue.AutoDelete,
                arguments: Queue.Arguments
                );

            var consumer = new AsyncEventingBasicConsumer(Channel);
            consumer.Received += HandleIncomingMessageAsync;

            Channel.BasicConsume(
                queue: Queue.QueueName,
                autoAck: false,
                consumer: consumer
                );
        }
        catch (Exception ex)
        {
            Logger.LogException(ex, LogLevel.Warning);
            await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
        }
    }
コード例 #20
0
ファイル: NinjectWebCommon.cs プロジェクト: tasinsahin/BLocal
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            // configure required providers using out-of-the-box implementations
            var localizationFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "localization.xml");
            var valueProvider        = new XmlValueProvider(localizationFilePath, true);
            var notifier             = new ExceptionNotifier();
            var localeProvider       = new MvcLocaleProvider("en", "nl");
            var partProvider         = new MvcPartProvider(new Part("General"));
            var logger = new VoidLogger();

            kernel.Bind <String>().ToConstant("flar");

            // bind all required providers as singletons
            kernel.Bind <ILocalizedValueProvider>().ToConstant(valueProvider).InSingletonScope();
            kernel.Bind <INotifier>().ToConstant(notifier).InSingletonScope();
            kernel.Bind <ILocaleProvider>().ToConstant(localeProvider).InSingletonScope();
            kernel.Bind <IPartProvider>().ToConstant(partProvider).InSingletonScope();
            kernel.Bind <ILocalizationLogger>().ToConstant(logger).InSingletonScope();

            // set up the repository and context, this is actually our goal here!
            kernel.Bind <LocalizationRepository>().ToSelf().InRequestScope();
            kernel.Bind <ILocalizationContext>().To <AlwaysDebuggingContext>().InRequestScope();
        }
コード例 #21
0
        protected virtual async Task TryCreateChannelAsync()
        {
            await DisposeChannelAsync();

            try
            {
                Channel = ConnectionPool
                          .Get(ConnectionName)
                          .CreateModel();

                Channel.ExchangeDeclare(
                    exchange: Exchange.ExchangeName,
                    type: Exchange.Type,
                    durable: Exchange.Durable,
                    autoDelete: Exchange.AutoDelete,
                    arguments: Exchange.Arguments
                    );

                if (!Exchange.DeadLetterExchangeName.IsNullOrWhiteSpace() &&
                    !Queue.DeadLetterQueueName.IsNullOrWhiteSpace())
                {
                    Channel.ExchangeDeclare(
                        Exchange.DeadLetterExchangeName,
                        Exchange.Type,
                        Exchange.Durable,
                        Exchange.AutoDelete
                        );

                    Channel.QueueDeclare(
                        Queue.DeadLetterQueueName,
                        Queue.Durable,
                        Queue.Exclusive,
                        Queue.AutoDelete);

                    Queue.Arguments["x-dead-letter-exchange"]    = Exchange.DeadLetterExchangeName;
                    Queue.Arguments["x-dead-letter-routing-key"] = Queue.DeadLetterQueueName;

                    Channel.QueueBind(Queue.DeadLetterQueueName, Exchange.DeadLetterExchangeName, Queue.DeadLetterQueueName);
                }

                var result = Channel.QueueDeclare(
                    queue: Queue.QueueName,
                    durable: Queue.Durable,
                    exclusive: Queue.Exclusive,
                    autoDelete: Queue.AutoDelete,
                    arguments: Queue.Arguments
                    );

                var consumer = new AsyncEventingBasicConsumer(Channel);
                consumer.Received += HandleIncomingMessageAsync;

                Channel.BasicConsume(
                    queue: Queue.QueueName,
                    autoAck: false,
                    consumer: consumer
                    );
            }
            catch (Exception ex)
            {
                if (ex is OperationInterruptedException operationInterruptedException &&
                    operationInterruptedException.ShutdownReason.ReplyCode == 406 &&
                    operationInterruptedException.Message.Contains("arg 'x-dead-letter-exchange'"))
                {
                    Exchange.DeadLetterExchangeName = null;
                    Queue.DeadLetterQueueName       = null;
                    Queue.Arguments.Remove("x-dead-letter-exchange");
                    Queue.Arguments.Remove("x-dead-letter-routing-key");
                    Logger.LogWarning("Unable to bind the dead letter queue to an existing queue. You can delete the queue or add policy. See: https://www.rabbitmq.com/parameters.html");
                }

                Logger.LogException(ex, LogLevel.Warning);
                await ExceptionNotifier.NotifyAsync(ex, logLevel : LogLevel.Warning);
            }
        }
コード例 #22
0
        /// <summary>
        /// Shows window with error message.
        /// </summary>
        /// <param name="exception">Exception to show.</param>
        public static void Show(System.Exception exception)
        {
            ExceptionNotifier notifier = new ExceptionNotifier(exception);

            notifier.ShowDialog();
        }