Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MassTransit - Example os simple client service
// **********************************************
// Fore more information, visit:
// Blog: https://blog.hildenco.com/2018/08/masstransit-real-alternative-to.html
// Source: https://github.com/MassTransit/Sample-Direct
// **********************************************
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Contracts;
using MassTransit;
using RabbitMQ.Client;
namespace DirectClient
{
class Program
{
static async Task Main()
{
var nodeId = Process.GetCurrentProcess().Id.ToString();
var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host("localhost", "/");
cfg.ConfigureMessageTopology();
cfg.ReceiveEndpoint(host, $"direct.client.{nodeId}", endpoint =>
{
endpoint.BindMessageExchanges = false;
endpoint.Bind<ContentReceived>(x =>
{
x.RoutingKey = nodeId;
x.ExchangeType = ExchangeType.Direct;
});
endpoint.Handler<ContentReceived>(async context => { Console.WriteLine("Content Received: {0}", context.Message.Id); });
});
});
await bus.StartAsync();
try
{
await bus.Publish<ClientAvailable>(new {NodeId = nodeId});
await Task.Run(() =>
{
Console.WriteLine("Press <enter> to quit..");
Console.ReadLine();
});
}
finally
{
await bus.StopAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.