Created
February 10, 2020 21:58
-
-
Save hd9/23ed30ece856b543e066f630e080c40b to your computer and use it in GitHub Desktop.
MassTransit - Example os simple client service
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
// ********************************************** | |
// 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