Я пытаюсь создать консольное приложение .net core 2.0 для подключения к экземпляру rabbitmq в PCF.Я использую последние стальные разъемы 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" />
Любая идея, как подключиться к экземпляру rabbitmq, размещенному в PCF, из консольного приложения.Примеры Steeltoe в основном являются образцами ядра asp.net.