订阅者部分:
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();
}
}