diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs index 0f48fae..286eff5 100644 --- a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueue.cs @@ -3,29 +3,36 @@ using RabbitMQ.Client; namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; -public class RabbitMQEventQueue : EventQueue +public class RabbitMQEventQueue : EventQueue { - public string Hostname { get; set; } = "localhost"; - public int Port { get; set; } = 5672; - public string Username { get; set; } = null!; - public string Password { get; set; } = null!; - public string ExchangeName { get; set; } = null!; - public string QueuePrefix { get; set; } = null!; - public override void Setup(IServiceCollection services) + protected override void Setup(IServiceCollection services, IDictionary connectionParameters) { + if (!connectionParameters.TryGetValue("Hostname", out string hostname)) + { + hostname = "localhost"; + } + if (!connectionParameters.TryGetValue("Port", out string port)) + { + port = "5672"; + } + string username = connectionParameters["Username"]; + string password = connectionParameters["Password"]; + string exchangeName = connectionParameters["ExchangeName"]; + string queuePrefix = connectionParameters["QueuePrefix"]; + ConnectionFactory factory = new ConnectionFactory { - HostName = Hostname, - Port = Port, - UserName = Username, - Password = Password, + HostName = port, + Port = int.Parse(port), + UserName = username, + Password = password, }; - + Task createConnectionTask = factory.CreateConnectionAsync(); createConnectionTask.Wait(); services.AddSingleton(createConnectionTask.Result); - services.AddSingleton(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService(), ExchangeName)); - services.AddSingleton(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService(), ExchangeName, QueuePrefix)); + services.AddSingleton(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService(), exchangeName)); + services.AddSingleton(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService(), exchangeName, queuePrefix)); } } \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueBuilder.cs b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueBuilder.cs new file mode 100644 index 0000000..f859166 --- /dev/null +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/RabbitMQEventQueueBuilder.cs @@ -0,0 +1,21 @@ +namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; + +public class RabbitMQEventQueueBuilder : EventQueueBuilder +{ + public string Hostname { get; set; } = "localhost"; + public int Port { get; set; } = 5672; + public string Username { get; set; } = null!; + public string Password { get; set; } = null!; + public string ExchangeName { get; set; } = null!; + public string QueuePrefix { get; set; } = null!; + + public override IDictionary GetConnectionParameters() => new Dictionary() + { + { nameof(Hostname), Hostname }, + { nameof(Port), Port.ToString() }, + { nameof(Username), Username }, + { nameof(Password), Password }, + { nameof(ExchangeName), ExchangeName }, + { nameof(QueuePrefix), QueuePrefix } + }; +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj index 3277958..c3ca9ef 100644 --- a/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj +++ b/TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj @@ -14,7 +14,7 @@ - +