I am trying to create a .net core 2.0 console application to connect to a rabbitmq instance in PCF. I am using the latest steeltoe connectors 2.1.0. Unfortunately I am not able to connect using AddRabbitMQConnection() and getting below exception when running in PCF. Basically it is not connection, but configuring ConnectionFactory with user provided service.
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)
My console application code looks like below
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>();
}
}
I am using below packages
<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" />
Any idea how to connect to PCF hosted rabbitmq instance from a console application. Steeltoe examples are mostly asp.net core samples.
Steeltoe 2.1.1 includes a fix for parsing of some environment variables, notably the PORT variable that sometimes resulted in this exception. Please try updating to that newest release to see if that resolves your issue.