我需要帮助,我有两个通过RawRabbit
进行通信的控制台应用程序。
我首先编写了一个控制台应用程序,并添加了Publisher
和Receiver
以查看连接是否正在发生:
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 });`
这很有效。
现在我想将接收器移动到另一个控制台应用程序,当我这样做时,它不起作用。
答案 0 :(得分:2)
用户部分:
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();
}
}