示例#1
0
        private object ResolveExpression(string value, Type expectedType, string propertyName)
        {
            var resolvedValue = Resolve(value);

            // TODO: This needs to handle #{} with beanreferences '@'
            if (ConfigUtils.IsExpression(resolvedValue))
            {
                var expression = ConfigUtils.ExtractExpressionString(resolvedValue);

                // could be prop placeholder inside #{ ${foo:bar?xyz} }
                expression = Resolve(expression);
                if (expectedType == typeof(string))
                {
                    return(expression);
                }
                else if (expectedType == typeof(int))
                {
                    // We assume its an int literal: eg. #{100}
                    if (int.TryParse(expression, out var result))
                    {
                        return(result);
                    }
                }
                else if (expectedType.IsEnum)
                {
                    var result = Enum.Parse(expectedType, expression, true);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                else if (expectedType == typeof(bool) && bool.TryParse(expression, out bool result))
                {
                    return(result);
                }

                // Handle #{@bean} and #{@bean}
                if (ApplicationContext == null)
                {
                    throw new InvalidOperationException("Unable to resolve expressions, no ApplicationContext present");
                }

                // Strip @ if present
                var servicename = ConfigUtils.ExtractServiceName(expression);

                // use service name to lookup
                var reference = ApplicationContext.GetService(servicename, expectedType);
                if (reference != null)
                {
                    return(reference);
                }

                throw new InvalidOperationException(string.Format("Unable to resolve expression {0} for RabbitListener property {1}", value, propertyName));
            }

            return(resolvedValue);
        }
        private static QueueBinding BuildBinding(string bindingName, DeclareQueueBindingAttribute b, IConfiguration config, string routingKey = null)
        {
            var binding = new QueueBinding(bindingName);

            if (!string.IsNullOrEmpty(b.QueueName))
            {
                var reference = PropertyPlaceholderHelper.ResolvePlaceholders(b.QueueName, config);
                if (ConfigUtils.IsExpression(reference))
                {
                    reference = ConfigUtils.ExtractExpressionString(reference);
                    if (ConfigUtils.IsServiceReference(reference))
                    {
                        reference = ConfigUtils.ExtractServiceName(reference);
                    }
                }

                if (_queueDeclss.TryGetValue(reference, out var queueRef))
                {
                    reference = queueRef.QueueName;
                }

                binding.Destination = reference;
            }

            if (!string.IsNullOrEmpty(b.ExchangeName))
            {
                binding.Exchange = PropertyPlaceholderHelper.ResolvePlaceholders(b.ExchangeName, config);
            }
            else
            {
                binding.Exchange = string.Empty;
            }

            if (!string.IsNullOrEmpty(b.IgnoreDeclarationExceptions))
            {
                binding.IgnoreDeclarationExceptions = GetBoolean(b.IgnoreDeclarationExceptions, config, nameof(b.IgnoreDeclarationExceptions));
            }

            if (!string.IsNullOrEmpty(b.Declare))
            {
                binding.ShouldDeclare = GetBoolean(b.Declare, config, nameof(b.Declare));
            }

            if (!string.IsNullOrEmpty(routingKey))
            {
                binding.RoutingKey = PropertyPlaceholderHelper.ResolvePlaceholders(routingKey, config);
            }
            else
            {
                binding.RoutingKey = string.Empty;
            }

            if (b.Admins.Length > 0)
            {
                foreach (var a in b.Admins)
                {
                    binding.DeclaringAdmins.Add(a);
                }
            }

            return(binding);
        }