C
C#6mo 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();
}
}
}
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();
}
}
}
No description
18 Replies
Pobiega
Pobiega6mo ago
And what are your values for those three variables?
HAHA
HAHA6mo ago
Let me get them I haven't added a try/catch
Pobiega
Pobiega6mo ago
Don't need one, just set a breakpoint and use the debugger?
HAHA
HAHA6mo ago
ok host "10.8.3.85" string
HAHA
HAHA6mo ago
No description
HAHA
HAHA6mo ago
here in the env variables
Pobiega
Pobiega6mo ago
ah I think the @ fucks you over since your string becomes root:Admin@123@10.8.3.85
HAHA
HAHA6mo ago
might be the case
Pobiega
Pobiega6mo ago
you'll need to escape it I guess, or change the password
HAHA
HAHA6mo ago
Uri = new Uri(@"amqp:/{username}:{password}@{host}")
Uri = new Uri(@"amqp:/{username}:{password}@{host}")
went for this instead minor case of interpolation
Pobiega
Pobiega6mo ago
hm will that work thou? no interpolation there that string will become literally amqp:/{username}:{password}@{host}
HAHA
HAHA6mo ago
yeah just noticed that I meant to write that as it is
Uri = new Uri("amqp://root:Admin@123@10.8.3.85")
Uri = new Uri("amqp://root:Admin@123@10.8.3.85")
Pobiega
Pobiega6mo ago
not sure that will work either thou having 2 @ in that string will be problematic
HAHA
HAHA6mo ago
var encodedPassword = Uri.EscapeUriString(password);
var factory = new ConnectionFactory
{
Uri = new Uri($"amqp://{username}:{encodedPassword}@{host}")
};
var encodedPassword = Uri.EscapeUriString(password);
var factory = new ConnectionFactory
{
Uri = new Uri($"amqp://{username}:{encodedPassword}@{host}")
};
did this
Pobiega
Pobiega6mo ago
yep, thats better
HAHA
HAHA6mo ago
didnt work
Pobiega
Pobiega6mo ago
rip remove the @ from the password, or find an alternative to using a uri if escaping it wont work
HAHA
HAHA6mo ago
that was it it worked for test env thxowo