C#C
C#2y ago
HAHA

System.UriFormatException: 'Invalid URI: The hostname could not be parsed.'

using RabbitMQ.Client;
using System;

namespace RabbitMQ.Implementations
{
    public abstract class RabbitMQClient : IDisposable
    {
        protected IConnection _connection;
        protected IModel _channel;

        public RabbitMQClient()
        {
            var username = Environment.GetEnvironmentVariable("AMQP_USERNAME") ?? throw new Exception("Env var AMQP_USERNAME is not defined.");
            var password = Environment.GetEnvironmentVariable("AMQP_PASSWORD") ?? throw new Exception("Env var AMQP_PASSWORD is not defined.");
            var host = Environment.GetEnvironmentVariable("AMQP_HOST") ?? throw new Exception("Env var AMQP_HOST is not defined.");

            var factory = new ConnectionFactory
            {
                Uri = new Uri($"amqp://{username}:{password}@{host}")
            };

            _connection = factory.CreateConnection();
            _channel = _connection.CreateModel();
        }

        public void Dispose()
        {
            _connection.Dispose();
            _channel.Dispose();
        }
    }
}
image.png
Was this page helpful?