Skip to content

Instantly share code, notes, and snippets.

@hd9
Created February 10, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hd9/23ed30ece856b543e066f630e080c40b to your computer and use it in GitHub Desktop.
Save hd9/23ed30ece856b543e066f630e080c40b to your computer and use it in GitHub Desktop.
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