Exemple #1
0
        static IMessageQueueFactory CreateMessageFactory(ILogger logger)
        {
            var rabbitMqHost = ConfigurationManager.AppSettings["RabbitMqHost"];

            if (string.IsNullOrWhiteSpace(rabbitMqHost))
            {
                logger.Warn("RabbitMQ Host has not been configured. All queue based operations will not be performed.");
                return(MessageQueueFactory.CreateInactiveFactory());
            }

            var messageFactory = new MessageQueueFactory(rabbitMqHost);

            try
            {
                logger.TraceFormat("Attempt to open connection to RabbitMQ on host \"{0}\".", rabbitMqHost);
                messageFactory.OpenConnection();
            }
            catch (IOException ex)
            {
                logger.ErrorFormat(ex,
                                   "An error occurred while opening a connection to RabbitMQ on host \"{0}\".", rabbitMqHost);
            }

            if (messageFactory.IsOpen)
            {
                return(messageFactory);
            }

            logger.WarnFormat(
                "Unable to open a connection to RabbitMQ Host \"{0}\". All queue based operations will not be performed.",
                rabbitMqHost);

            return(MessageQueueFactory.CreateInactiveFactory());
        }