Ejemplo n.º 1
0
    public static Broker GetBroker()
    {
        var connectionString = Environment.GetEnvironmentVariable("RabbitMQTransport_ConnectionString");

        if (string.IsNullOrEmpty(connectionString))
        {
            throw new Exception("The 'RabbitMQTransport_ConnectionString' environment variable is not set.");
        }

        var connectionStringBuilder = new DbConnectionStringBuilder {
            ConnectionString = connectionString
        };

        string hostName;

        if (connectionStringBuilder.TryGetValue("host", out var value))
        {
            hostName = value.ToString();
        }
        else
        {
            throw new Exception("The connection string doesn't contain a value for 'host'.");
        }

        return(new Broker
        {
            UserName = connectionStringBuilder.GetOrDefault("username", "guest"),
            Password = connectionStringBuilder.GetOrDefault("password", "guest"),
            VirtualHost = connectionStringBuilder.GetOrDefault("virtualhost", "/"),
            HostName = hostName,
            Port = 15672,
        });
    }