© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
32 replies
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();
        }
    }
}
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Failed to create component; Invalid URI: The hostname could not be parsed
C#CC# / help
3y ago
❔ The project file could not be loaded. Data at the root level invalid
C#CC# / help
3y ago
The type name 'ServiceController' could not be found in the namespace 'System.ServiceProcess'.
C#CC# / help
4y ago
✅ could not be translated
C#CC# / help
17mo ago