【问题标题】:Asp .Net Core Web API where to subscribe RabbitMQAsp .Net Core Web API 在哪里订阅 RabbitMQ
【发布时间】:2022-04-04 05:12:41
【问题描述】:

我正在尝试使用 Web API 和 Rabbit MQ 消息代理来实现发布/订阅架构。 我的解决方案中有两个项目:发布者和订阅者。

发布正在成功实施,但我在我的 订阅者项目从队列中读取发布的消息。

我的两个项目都是 .Net Core ASP WEB API

提前致谢

【问题讨论】:

  • 你应该展示一些你迄今为止尝试过的代码
  • 感谢您的回复。我想我发现了这样做的最佳实践。我在 ConfigureServices 方法中使用 AddSingleton 方法将 rabbit 注册为 HostedService。 IHostedService 在内部调用 ApplicationGetStarted 事件。所以兔子开始在那里听
  • 您应该将其发布为您自己问题的答案,因为它帮助我解决了我的问题 (stackoverflow.com/questions/56360983/…)。

标签: asp.net-web-api rabbitmq


【解决方案1】:

使用 ConfigureServices 方法中的 AddSingleton 方法将 rabbitMq 注册为 HostedService。 IHostedService 在内部调用 ApplicationGetStarted 事件。于是兔子开始在那里听

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMassTransit(x =>
        {
            x.UsingRabbitMq();
        });

        // OPTIONAL, but can be used to configure the bus options
        services.AddOptions<MassTransitHostOptions>()
            .Configure(options =>
            {
                // if specified, waits until the bus is started before
                // returning from IHostedService.StartAsync
                // default is false
                options.WaitUntilStarted = true;

                // if specified, limits the wait time when starting the bus
                options.StartTimeout = TimeSpan.FromSeconds(10);

                // if specified, limits the wait time when stopping the bus
                options.StopTimeout = TimeSpan.FromSeconds(30);
            });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2018-07-26
    • 1970-01-01
    • 2019-01-24
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多