Created
March 26, 2020 18:26
-
-
Save dariogriffo/001a1119edf7da2cfd83c9975f1baac1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Autofac; | |
using RabbitMQ.Client.Framing.Impl; | |
using Rebus.Bus; | |
using Rebus.Config; | |
using Rebus.Handlers; | |
using Rebus.Routing.TypeBased; | |
namespace ConsoleApp16 | |
{ | |
public class Ping | |
{ | |
public Ping() | |
{ | |
Id = "THE MESSAGE"; | |
} | |
public string Id { get; set; } | |
} | |
public class PingHandler : IHandleMessages<Ping> | |
{ | |
public Task Handle(Ping message) | |
{ | |
Console.WriteLine(message.Id); | |
return Task.CompletedTask; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterType<PingHandler>().AsImplementedInterfaces(); | |
const string inputQueueName = "test"; | |
const string connectionString = "amqps://test:test@localhost"; | |
builder.RegisterRebus(x => | |
x.Transport(c => c.UseRabbitMq(connectionString, inputQueueName)) | |
.Events(e => e.AfterMessageHandled += (a, b, c, d, e) => | |
{ | |
Console.WriteLine("after"); | |
}) | |
.Routing(r => r.TypeBased()) | |
.Events(e => e.BeforeMessageHandled += (a, b, c, d, e) => | |
{ | |
Console.WriteLine("before"); | |
}) | |
); | |
using (var container = builder.Build()) | |
{ | |
var bus = container.Resolve<IBus>(); | |
bus.Subscribe<Ping>().GetAwaiter().GetResult(); | |
bus.Publish(new Ping()).GetAwaiter().GetResult(); | |
Thread.Sleep(TimeSpan.FromSeconds(5)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment