通过"RawRabbit"进行两个控制台应用程序通信



我需要帮助,我有两个通过RawRabbit进行通信的控制台应用程序。

我首先编写了一个控制台应用程序,并添加了PublisherReceiver以查看连接是否正在发生:

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

这行得通。

现在我想将接收器移动到另一个控制台应用程序,当我这样做时,它不起作用。

订阅者部分:

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

以及,发布者部分:

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

最新更新