无法通过 .net 核心控制台应用程序的 steeltoe 连接器连接到 RabbitMQ

Unable to connect to RabbitMQ via steeltoe connector from .net core console application

我正在尝试创建一个 .net core 2.0 控制台应用程序以连接到 PCF 中的 rabbitmq 实例。我正在使用最新的 steeltoe 连接器 2.1.0。不幸的是,我无法使用 AddRabbitMQConnection() 进行连接,并且在 PCF 中 运行 时出现以下异常。基本上不是连接,而是用用户提供的服务配置ConnectionFactory。

2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] Unhandled Exception: System.InvalidOperationException: Failed to convert '' to type 'System.Int32'. ---> System.Exception: is not a valid value for Int32. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] --- End of inner exception stack trace ---
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Microsoft.Extensions.Configuration.ConfigurationBinder.TryConvertValue(Type type, String value, Object& result, Exception& error)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration configuration, Object instance)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Steeltoe.CloudFoundry.Connector.CloudFoundryServiceInfoCreator.BuildServiceInfos()
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Steeltoe.CloudFoundry.Connector.CloudFoundryServiceInfoCreator.Instance(IConfiguration config)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Steeltoe.CloudFoundry.Connector.IConfigurationExtensions.GetServiceInfos[SI](IConfiguration config)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Steeltoe.CloudFoundry.Connector.IConfigurationExtensions.GetSingletonServiceInfo[SI](IConfiguration config)
2018-10-09T17:02:58.849+05:30 [APP/TASK/execute-dlqprcoessing-task/0] [ERR] at Steeltoe.CloudFoundry.Connector.RabbitMQ.RabbitMQProviderServiceCollectionExtensions.AddRabbitMQConnection(IServiceCollection services, IConfiguration config, ServiceLifetime contextLifetime, ILoggerFactory logFactory)

我的控制台应用程序代码如下所示

 static void Main(string[] args)
        {
          var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            var cfgBuilder = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddEnvironmentVariables()
               .AddJsonFile("appsettings.json", true, false)
               .AddJsonFile($"appsettings.{envName}.json", true, false)
               .AddCloudFoundry();
            var configuration = cfgBuilder.Build();

            var serviceCollection = new ServiceCollection();

            // Configure services for DI
            ConfigureServices(serviceCollection, configuration);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var worker = serviceProvider.GetRequiredService<Worker>();
            Console.WriteLine("Started reading from queue");
            worker.Receieve();

            Console.WriteLine("Finished executing task!");
        }

        private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            // add logging
            services.AddSingleton(new LoggerFactory()
            .AddConsole(configuration.GetSection("Logging"))
            .AddDebug());

            // Read the configuration from app settings. If not found,  use pcf version of rabbitmq
            var connectionConfig = configuration.GetSection("ConnectionFactory").Get<RabbitMqConnectionConfig>();
            if (connectionConfig != null)
            {
                var factory = new ConnectionFactory
                {
                    VirtualHost = connectionConfig.VirtualHost,
                    HostName = connectionConfig.HostName,
                    Port = Convert.ToInt32(connectionConfig.Port),
                    UserName = connectionConfig.UserName,
                    Password = connectionConfig.Password
                };
                services.AddSingleton<IConnectionFactory>(c => factory);
            }
            else
            {
                // use the steel-toe connector
                Console.WriteLine("Trying to connect to RabbitMQ via steeltoe connector!");
                services.AddRabbitMQConnection(configuration);
                Console.WriteLine("Connected to RabbitMQ via steeltoe connector!");
            }

            // add worker
            services.AddSingleton<Worker>();
        }
    }

我正在使用以下软件包

 <PackageReference Include="Steeltoe.Extensions.Configuration.CloudFoundryCore" Version="2.1.0" />
 <PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
 <PackageReference Include="Steeltoe.CloudFoundry.ConnectorCore" Version="2.1.0" />

知道如何从控制台应用程序连接到 PCF 托管的 rabbitmq 实例。 Steeltoe 示例主要是 asp.net 个核心示例。

Steeltoe 2.1.1 包含对某些环境变量解析的修复,尤其是有时会导致此异常的 PORT 变量。请尝试更新到最新版本,看看是否能解决您的问题。