コード例 #1
0
        public static void Validate(BrokerFactoryConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (string.IsNullOrWhiteSpace(configuration.Url))
            {
                if (string.IsNullOrWhiteSpace(configuration.HostName))
                {
                    throw new ArgumentException($"{nameof(configuration.HostName)} is missing.");
                }

                if (configuration.HostPort <= 0)
                {
                    throw new ArgumentException($"{nameof(configuration.HostPort)} is missing.");
                }

                if (string.IsNullOrWhiteSpace(configuration.UserName))
                {
                    throw new ArgumentException($"{nameof(configuration.UserName)} is missing.");
                }

                if (string.IsNullOrWhiteSpace(configuration.Password))
                {
                    throw new ArgumentException($"{nameof(configuration.Password)} is missing.");
                }
            }

            if (configuration.NetworkRecoveryIntervalSeconds < 0)
            {
                throw new ArgumentException($"{nameof(configuration.NetworkRecoveryIntervalSeconds)} can not be less than 0.");
            }

            if (string.IsNullOrWhiteSpace(configuration.DefaultDeadLetterExchange))
            {
                throw new ArgumentException($"{nameof(configuration.DefaultDeadLetterExchange)} is missing.");
            }

            if (string.IsNullOrWhiteSpace(configuration.DefaultDeadLetterQueue))
            {
                throw new ArgumentException($"{nameof(configuration.DefaultDeadLetterQueue)} is missing.");
            }
        }
コード例 #2
0
        /// <summary>
        /// BrokerFactory constructor.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown, if the configuration is wrong.</exception>
        /// <exception cref="ArgumentException">Thrown, if the configuration is wrong.</exception>
        public BrokerFactory(
            BrokerFactoryConfiguration configuration,
            IAttributeProvider <MessageSettingsAttribute> attributeProvider = null)
        {
            BrokerFactoryConfiguration.Validate(configuration);

            if (string.IsNullOrWhiteSpace(configuration.Url))
            {
                _connectionFactory = new ConnectionFactory
                {
                    HostName    = configuration.HostName,
                    Port        = configuration.HostPort,
                    VirtualHost = configuration.VirtualHost,
                    UserName    = configuration.UserName,
                    Password    = configuration.Password,
                    AutomaticRecoveryEnabled = configuration.NetworkRecoveryIntervalSeconds > 0,
                    NetworkRecoveryInterval  = TimeSpan.FromSeconds(configuration.NetworkRecoveryIntervalSeconds)
                };
            }
            else
            {
                _connectionFactory = new ConnectionFactory
                {
                    Uri = new Uri(configuration.Url),
                    AutomaticRecoveryEnabled = configuration.NetworkRecoveryIntervalSeconds > 0,
                    NetworkRecoveryInterval  = TimeSpan.FromSeconds(configuration.NetworkRecoveryIntervalSeconds)
                };
            }

            _factoryConfiguration = configuration;

            _attributeProvider = attributeProvider ?? new SimpleAttributeProvider <MessageSettingsAttribute>();

            _lazyConnection = new Lazy <IConnection>(createConnection);

            _attributesDic = new ConcurrentDictionary <Type, MessageSettingsAttribute>();
        }