0

我正在尝试创建一个 .net core 2.0 控制台应用程序以连接到 PCF 中的 rabbitmq 实例。我正在使用最新的钢趾连接器 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 核心示例。

4

1 回答 1

1

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

于 2018-10-24T14:41:37.270 回答