RawRabbit订阅服务器在ASP.NET Core中不起作用

时间:2018-07-22 04:41:56

标签: c# asp.net-web-api rabbitmq publish-subscribe rawrabbit

我正在asp.net核心中创建RestApi,并且在我的一项服务中,我使用RawRabbit将消息发布到RabbitMQ。就是说,当我对订户部分进行注释时,我看到消息已发布在RabbitMQ控制面板中,使用方的数量为0,当我添加订户部分时,使用方的数量变为1并且消息被使用,因此在控制面板中没有消息,但是奇怪的是,订户中的所有代码(在这种情况下只是一个日志)都没有运行。

发布者部分:

public  async void RaiseAsync(string event_name, ShoppingCartItemAdded data) {

        const string EXCHANGE_NAME = "myRabbit";

        Action<IPublishContext> x = (ctx) => ctx.UsePublishConfiguration(xfg => xfg.OnExchange(EXCHANGE_NAME));//.WithRoutingKey("shoppingcartitemadded"));

        await this.Client.PublishAsync<ShoppingCartItemAdded>(data, x );
    }

用户部分:

 public class ShoppingCartItemAddedConsumer
{
    private readonly ILogger<ShoppingCartItemAddedConsumer> logger;
    private readonly IBusClient client;

    public ShoppingCartItemAddedConsumer(ILogger<ShoppingCartItemAddedConsumer> logger, IBusClient client)
    {
        this.logger = logger;
        this.client = client;

        this.logger.LogInformation("Subscriber created");
    }

    public async void Run()
    {
        const string QUEUE_NAME = "myWebApi";
        const string EXCHANGE_NAME = "myRabbit";

        this.logger.LogInformation("Registering subscriber");

        await client.SubscribeAsync<ShoppingCartItemAdded>(async msg => {

            this.logger.LogInformation("Message received from rabbitmq : {message}", msg);

        }, ctx => ctx.UseSubscribeConfiguration(cfg =>
            cfg.OnDeclaredExchange(dex => dex.WithName(EXCHANGE_NAME)
                .WithAutoDelete(false)
                .WithDurability(true)
                .WithType(ExchangeType.Topic)
            )
            .FromDeclaredQueue(dq => dq.WithName(QUEUE_NAME)
                .WithExclusivity(false)
                .WithDurability(true)
                .WithAutoDelete(false))));

        this.logger.LogInformation("Subscriber registered");
    }
}

然后我将订户注册为单例服务,我在Startup.cs中将其称为Run()方法

var consumer = app.ApplicationServices.GetRequiredService<ShoppingCartItemAddedConsumer>();
        consumer.Run();

这是服务启动时的日志:

[09:02:25 INF] Subscriber created
[09:02:26 INF] Registering subscriber
[09:02:26 INF] Configuration action for shoppingcartitemadded found.
[09:02:26 INF] Declaring queue myWebApi.
[09:02:26 INF] Declaring exchange myRabbit.
[09:02:26 INF] Binding queue myWebApi to exchange myRabbit with routing key shoppingcartitemadded
[09:02:26 INF] Preparing to consume message from queue 'myWebApi'.
[09:02:26 INF] Subscriber registered

,以下是发布消息时的日志:

[14:13:35 INF] Setting 'Publish Acknowledge' for channel '3'
[14:13:35 INF] Setting up publish acknowledgement for 1 with timeout 0:00:01
[14:13:35 INF] Sequence 1 added to dictionary
[14:13:35 WRN] No body found in the Pipe context.
[14:13:35 INF] Performing basic publish with routing key shoppingcartitemadded on exchange myRabbitAPI.
[14:13:35 INF] Setting up publish acknowledgement for 2 with timeout 0:00:01
[14:13:35 INF] Sequence 2 added to dictionary
[14:13:35 WRN] No body found in the Pipe context.
[14:13:35 INF] Performing basic publish with routing key shoppingcartitemadded on exchange myRabbitAPI.
[14:13:35 INF] Executed action method HelloMicroServices.Controllers.ShoppingCartController.Post (HelloMicroServices), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 304.6292ms.
[14:13:35 INF] Executing ObjectResult, writing value of type 'HelloMicroServices.Datastores.Models.ShoppingCart'.
[14:13:35 INF] Executed action     HelloMicroServices.Controllers.ShoppingCartController.Post (HelloMicroServices) in 414.4309ms
[14:13:35 INF] Request finished in 475.1868ms 200 application/json; charset=utf-8
[14:13:35 INF] Recieived ack for 1
[14:13:35 INF] Recieived ack for 2

0 个答案:

没有答案