0

I have implemented a .net core web api with masstransit rabbitMQ and signalR and im trying to make signalR backplane work. I suspect the problem is with exchange bindings that masstransit solves for me but i cant figure out what im doing wrong to have this behaviour.

My goal here is to be able to have an endpoint that another app will call (for now im using postman for this) that will publish a message to all the clients (js side) but whenever the message is published and it arrives to the exchange nothing happens since there is no binding.

In rabbitMQ management tools it creates an exchange without any binding to another exchange/queue

enter image description here

My startup:

        public void ConfigureServices(IServiceCollection services)
    {
        Utilities utilities = new Utilities(Configuration);

        RabbitMQIdentity rabbitMQIdentity = utilities.GetRabbitMQIdentity();
        var username = rabbitMQIdentity.UserName;
        var password = rabbitMQIdentity.Password;
        var hostName = rabbitMQIdentity.HostName;
        var portNumber = rabbitMQIdentity.Port;

        services.AddHttpClient();
        services.AddControllers();
        services.AddSignalR();

        services.AddMassTransit(config =>
        {
            config.AddSignalRHub<NotificationHub>();
            config.UsingRabbitMq((ctx, cfg) =>
            {
                cfg.Host($"amqp://{username}:{password}@{hostName}:{portNumber}");
                cfg.ConfigureEndpoints(ctx);
            });
        });
        services.AddMassTransitHostedService();

        services.AddSingleton<IHostEnvironment>(hostEnvironment);
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<LogConfigurationUtility, WebLogConfigurationUtility>();
        services.AddCors(options =>
        {
            options.AddDefaultPolicy(builder =>
            {
                builder.SetIsOriginAllowed((x) => Configuration["CorsWhiteList"].Split(';').Any(x.Contains))
                       .WithMethods("GET", "POST")
                       .AllowAnyHeader()
                       .AllowCredentials();
            });
        });
    }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCors();

        app.UseMiddleware<RequestMiddleware>();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<NotificationHub>("/notificationhub");
        });
    }

My publish method:

IReadOnlyList<IHubProtocol> protocols = new IHubProtocol[] { new JsonHubProtocol() };


        publishEndpoint.Publish<All<NotificationHub>>(new
        {
            Messages = protocols.ToProtocolDictionary("Notify", new object[] { "backend-process", "oiiiii" })
        });

And my client side:

useEffect(() => {
    $(function() {
        const connection = new HubConnectionBuilder()
            .withUrl(hubUrl)
            .configureLogging(LogLevel.Trace)
            .build();
        // Create a function that the hub can call to broadcast messages.
        connection.on("Notify", (status) => {
            console.log("entrouuuuuu");
            setNotification(status);
        });
        // Start the connection.
        async function start() {
            try {
                await connection.start();
                connection.invoke("InitializeClient", orderId);
                console.log("SignalR Connected.");
            } catch (err) {
                setTimeout(start, 5000);
            }
        }

        start();
    });
}, []);

I have followed the official MassTransit documentation and tried to figure out what the sample example differs from mine but i cant seem to be able to figure it out (apart from creating and binding the exchanges correctly).

https://masstransit-project.com/advanced/signalr/quickstart.html https://masstransit-project.com/advanced/signalr/sample.html

Could anyone help me please?

1 Answers1

0

There is a bug in 7.1.6 that has been fixed and will be released in 7.1.7.

Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • Thank you so much! i downgraded my version to 6.3.2 while 7.1.7 is not out yet and it worked like a charm =) Another question if i may though, how am i able to tell MassTransit the group i want to send a message to when i peform a Publish? – João Cardoso Mar 11 '21 at 11:01
  • No clue, you might actually post another question or search for the answer. – Chris Patterson Mar 11 '21 at 12:34