Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Masstransit - Rabbit MQ virtual host

I am using mass transit and connecting to my rabbit broker.

 string uri1 = @"rabbitmq://myusername:mypassword@myip/myvirtualhost/myqueue";

I keep getting a ConfigurationException was unhandled.

An exception was thrown during service bus creation {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} {"Failed to start bus services"} at MassTransit.ServiceContainer.Start() in d:\BuildAgent-03\work\aa063b4295dfc097\src\MassTransit\ServiceContainer.cs:line 83 at MassTransit.ServiceBus.Start() in d:\BuildAgent-03\work\aa063b4295dfc097\src\MassTransit\ServiceBus.cs:line 369 at MassTransit.Builders.ServiceBusBuilderImpl.Build() in d:\BuildAgent-03\work\aa063b4295dfc097\src\MassTransit\Configuration\Builders\ServiceBusBuilderImpl.cs:line 84 at MassTransit.BusConfigurators.ServiceBusConfiguratorImpl.CreateServiceBus() in d:\BuildAgent-03\work\aa063b4295dfc097\src\MassTransit\Configuration\BusConfigurators\ServiceBusConfiguratorImpl.cs:line 171 at MassTransit.ServiceBusFactory.New(Action`1 configure) in d:\BuildAgent-03\work\aa063b4295dfc097\src\MassTransit\Configuration\ServiceBusFactory.cs:line 44

What really odd is that it works without specifying the virtual host but them offcourse goes to the wrong virtual host.

I then downloaded the rabbitMQ library and it connected fine to the right virtual host with my credentials. This is how I know my credentials, virtual host is set up fine. I even added a queue in case that was the problem. Is there some bug in Masstransit? I really dont see what im doing wrong. I'm contemplating not using masstransit and programming my own lightweight version.

I even copied pasted this into https://github.com/MassTransit/MassTransit/blob/v2.7.2/src/Transports/MassTransit.Transports.RabbitMq/RabbitMqEndpointAddress.cs#L167 my solution to check if it is trimming the virtual host correctly and it is. I'm really confused. ` static readonly Regex regex = new Regex(@"^[A-Za-z0-9-.:]+$");

    public static RabbitMqEndpointAddress Parse(Uri address)
    {
        Guard.AgainstNull(address, "address");

        if (string.Compare("rabbitmq", address.Scheme, true) != 0)
            throw new RabbitMqAddressException("The invalid scheme was specified: " + address.Scheme ?? "(null)");

        var connectionFactory = new ConnectionFactory
        {
            HostName = address.Host,
            UserName = "",
            Password = "",
        };

        if (address.IsDefaultPort)
            connectionFactory.Port = 5672;
        else if (!address.IsDefaultPort)
            connectionFactory.Port = address.Port;

        if (!address.UserInfo.IsEmpty())
        {
            if (address.UserInfo.Contains(":"))
            {
                string[] parts = address.UserInfo.Split(':');
                connectionFactory.UserName = parts[0];
                connectionFactory.Password = parts[1];
            }
            else
                connectionFactory.UserName = address.UserInfo;
        }

        string name = address.AbsolutePath.Substring(1);
        string[] pathSegments = name.Split('/');
        if (pathSegments.Length == 2)
        {
            connectionFactory.VirtualHost = pathSegments[0];
            name = pathSegments[1];
        }

        ushort heartbeat = address.Query.GetValueFromQueryString("heartbeat", connectionFactory.RequestedHeartbeat);
        connectionFactory.RequestedHeartbeat = heartbeat;

       VerifyQueueOrExchangeNameIsLegal(name);

        return new RabbitMqEndpointAddress(address, connectionFactory, name);
    }
    static void VerifyQueueOrExchangeNameIsLegal(string path)
    {
        Match match = _regex.Match(path);

        if (!match.Success)
            throw new RabbitMqAddressException(FormatErrorMsg);
    }

    const string FormatErrorMsg =
      "The path can be empty, or a sequence of these characters: letters, digits, hyphen, underscore, period, or colon.";


    private static void Main(string[] args)
    {

        string uri1 = @"rabbitmq://username:[email protected]/vhost/queue";
        IServiceBus serviceBus;

        var result =  Parse(new Uri(uri1));

        serviceBus = ServiceBusFactory.New(sbc =>
        {
            sbc.UseRabbitMq();
            sbc.ReceiveFrom(uri1);
            sbc.Subscribe(c => c.Consumer<SendEmailConsumer>());
        });

        IPublish publishMessage = new MassTransitPublisher(uri1);

        publishMessage.Publish(new SendEmail
            {
                EmailFrom = "*@8.co.za",
                EmailTo = "*@*.co.za",
                Subject = "hello",
                Body = "hello"
            });

        Console.ReadKey();


        JobRunner jobRunner = new JobRunner();

        jobRunner.Start();

        Console.ReadKey();
    }`

Any help would be appreciated or anything else I can try to get this working.

like image 439
Guculuma Avatar asked Oct 26 '25 20:10

Guculuma


1 Answers

What version of MassTransit are you using?

We removed the ability to declare the username and password from the Uri (so it doesn't appear in the logs) and you must do it via the API.

See https://groups.google.com/d/msg/masstransit-discuss/4m5Vf04oRWM/hujvVh1HSdwJ.

sbc.UseRabbitMq(x => 
                x.ConfigureHost("rabbitmq://yourhost/yourvhost/yourqueue", 
                x=> x.UserName, 
                x.Password))
like image 110
Travis Avatar answered Oct 28 '25 10:10

Travis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!