0

I need help, I have two console application that communicate via RawRabbit.

I first wrote one console applications and added both the Publisher and the Receiver to see if the connection is happening:

var busClient = BusClientFactory.CreateDefault(busConfig);

busClient.SubscribeAsync<UserMessage>(async (resp, context) => {
     Console.Clear();
     Console.WriteLine(resp.msg);
     Console.WriteLine("Hi {0}, I am your father.", resp.name);
});
busClient.PublishAsync(new UserMessage { msg = "Hello my name is, " + name, name = name });`

This works.

Now I want to move the receiver to another console application, When I do that, it does not work.

Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52

1 Answers1

2

Subscriber part:

class Program {

    class MyMessage {
        public int Id { get; set; }

        public string Message { get; set; }
    }

    static async Task Main(string[] args) {

        IBusClient client = RawRabbitFactory.CreateSingleton(
            new RawRabbitOptions {
                ClientConfiguration =
                new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build()
                    .Get<RawRabbitConfiguration>()
            });

        const string QUEUE_NAME = "myConsole";
        const string EXCHANGE_NAME = "myRabbit";


        await client.SubscribeAsync<MyMessage>(async msg => {
            await Task.Run(() => {
                Console.WriteLine("{0}: {1}", msg.Id, msg.Message);
            });
        }, 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))));
        Console.ReadLine();
    }
}

and, publisher part:

class Program {

    class MyMessage {
        public int Id { get; set; }

        public string Message { get; set; }
    }

    static async Task Main(string[] args) {

        IBusClient client = RawRabbitFactory.CreateSingleton(
            new RawRabbitOptions {
                ClientConfiguration =
                new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build()
                    .Get<RawRabbitConfiguration>()
            });

        const string QUEUE_NAME = "myConsole";
        const string EXCHANGE_NAME = "myRabbit";


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

        await client.PublishAsync<MyMessage>(new MyMessage { Id = 5, Message = "Hello RabbitMQ" }, x);
        await client.PublishAsync(new MyMessage { Id = 4, Message = "Hello RabbitMQ" }, x);

        Console.ReadLine();
    }
}
Saeed Ganji
  • 197
  • 17