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

时间:2018-10-09 11:51:20

标签: .net-core pcf steeltoe

我正在尝试创建一个.net core 2.0控制台应用程序以连接到PCF中的Rabbitmq实例。我正在使用最新的Steeltoe连接器2.1.0。不幸的是,当我在PCF中运行时,我无法使用AddRabbitMQConnection()进行连接并出现以下异常。基本上不是连接,而是使用用户提供的服务配置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核心示例。

1 个答案:

答案 0 :(得分:1)

Steeltoe 2.1.1包含一个用于解析某些环境变量的修复程序,尤其是有时导致此异常的PORT变量。请尝试更新到最新版本,看看是否可以解决您的问题。