首页 > 解决方案 > MassTransit SignalR 绑定

问题描述

我在设置 MassTransit SignalR 解决方案时遇到了一些问题。我之前发布了一个关于 Chris 回答并帮助我的绑定的问题,但是在更新包之后(我仍在使用 6.xx,它有其他问题,例如重新启动 RabbitMQ 服务会在发送消息时出现错误)我的绑定没有似乎工作正常了。

这是我问的第一个问题:Exchange binding not working in MassTransit with RabbitMQ and SignalR

现在我已经更新到 7.1.7 并且还更新了已弃用的方法。这就是我的 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(e => {
            e.EnableDetailedErrors = true;
            e.MaximumReceiveMessageSize = 102400000;
        });

        services.AddMassTransit(x =>
        {
            x.AddSignalRHub<NotificationHub>();

            x.UsingRabbitMq((context, cfg) =>
            {
                cfg.Host($"amqp://{username}:{password}@{hostName}:{portNumber}");

                cfg.AutoDelete = true;
                cfg.Durable = false;
                cfg.QueueExpiration = TimeSpan.FromMinutes(10);
                cfg.ConfigureEndpoints(context);
            });
        });

        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();
            });
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    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");
        });
    }

这是我的发布:

logger.LogInformation($"MassTransit publishing group message. GroupName:{paymentCallback.OrderId} ; Message:{paymentCallback.Event}");

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

publishEndpoint.Publish<Group<NotificationHub>>(
                new GroupHub<NotificationHub>()
                {
                    GroupName = paymentCallback.OrderId,
                    Messages = protocols.ToProtocolDictionary("Notify", new object[] { paymentCallback.Event })
                },
                context => context.TimeToLive = TimeSpan.FromMinutes(10)
            );

和客户:

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();
});

所以现在,在 RabbitMQ 管理工具中,我可以看到正在创建的交换,但它再次没有绑定,也没有队列。克里斯仍然存在同样的问题吗?还是我做错了什么?

在此处输入图像描述

谢谢您的帮助!

标签: .net-corerabbitmqsignalrmasstransit

解决方案


推荐阅读