Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting of individual properties from Uri in ConnectionFactory #35

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/HouseofCat.RabbitMQ/Options/FactoryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
using System;
using RabbitMQ.Client;

namespace HouseofCat.RabbitMQ
{
public class FactoryOptions
{
/// <summary>
/// ConnectionFactory (RabbitMQ) Uri connection string.
/// ConnectionFactory (RabbitMQ) Uri connection string. Set to null to use individual properties.
/// <para>amqp(s)://guest:guest@localhost:5672/vhost</para>
/// </summary>
public Uri Uri { get; set; } = new Uri("amqp://guest:guest@localhost:5672/");

/// <summary>
/// ConnectionFactory (RabbitMQ) virtual host property. Use in lieu of Uri connection string.
/// </summary>
public string VirtualHost { get; set; } = "";

/// <summary>
/// ConnectionFactory (RabbitMQ) username property. Use in lieu of Uri connection string.
/// </summary>
public string UserName { get; set; } = "guest";

/// <summary>
/// ConnectionFactory (RabbitMQ) password property. Use in lieu of Uri connection string.
/// </summary>
public string Password { get; set; } = "guest";

/// <summary>
/// ConnectionFactory (RabbitMQ) host name property. Use in lieu of Uri connection string.
/// </summary>
public string HostName { get; set; } = "localhost";

/// <summary>
/// ConnectionFactory (RabbitMQ) port property. Use in lieu of Uri connection string.
/// </summary>
public int Port { get; set; } = AmqpTcpEndpoint.UseDefaultPort;

/// <summary>
/// ConnectionFactory (RabbitMQ) max connection property.
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/HouseofCat.RabbitMQ/Pools/ConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private ConnectionFactory CreateConnectionFactory()
{
var cf = new ConnectionFactory
{
Uri = Options.FactoryOptions.Uri,
AutomaticRecoveryEnabled = true,
TopologyRecoveryEnabled = Options.FactoryOptions.TopologyRecovery,
NetworkRecoveryInterval = TimeSpan.FromSeconds(Options.FactoryOptions.NetRecoveryTimeout),
Expand All @@ -60,6 +59,22 @@ private ConnectionFactory CreateConnectionFactory()
DispatchConsumersAsync = Options.FactoryOptions.EnableDispatchConsumersAsync,
};

if (Options.FactoryOptions.Uri != null)
{
cf.Uri = Options.FactoryOptions.Uri;
}
else
{
cf.VirtualHost = Options.FactoryOptions.VirtualHost;
cf.HostName = Options.FactoryOptions.HostName;
cf.UserName = Options.FactoryOptions.UserName;
cf.Password = Options.FactoryOptions.Password;
if (Options.FactoryOptions.Port != AmqpTcpEndpoint.UseDefaultPort)
{
cf.Port = Options.FactoryOptions.Port;
}
}

if (Options.FactoryOptions.SslOptions.EnableSsl)
{
cf.Ssl = new SslOption
Expand Down