Created
January 9, 2018 20:31
-
-
Save alexeyzimarev/dd857685948baeaea02c11722967e364 to your computer and use it in GitHub Desktop.
MassTransit v4 and .NET Core console app to run in a Docker container
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.IO; | |
using System.Runtime.Loader; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using GreenPipes; | |
using MassTransit; | |
using Microsoft.Extensions.Configuration; | |
using Serilog; | |
namespace Abax.Events.Log | |
{ | |
internal static class Program | |
{ | |
private static IBusControl _busControl; | |
private static readonly AutoResetEvent WaitHandle = new AutoResetEvent(false); | |
internal static void Main(string[] args) | |
{ | |
AssemblyLoadContext.Default.Unloading += _ => Exit(); | |
Console.CancelKeyPress += (_, __) => Exit(); | |
Serilog.Log.Logger = new LoggerConfiguration() | |
.Enrich.WithProperty("name", typeof(Program).Assembly.GetName().Name) | |
.WriteTo.Console() | |
.CreateLogger(); | |
var config = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json") | |
.Build(); | |
var rmqSettings = config.GetSection("RabbitMq").Get<Configuration.RabbitMq>(); | |
_busControl = Bus.Factory.CreateUsingRabbitMq(cfg => | |
{ | |
cfg.PrefetchCount = 50; | |
cfg.UseConcurrencyLimit(50); | |
var host = cfg.Host(new Uri(rmqSettings.Host), h => | |
{ | |
h.Username(rmqSettings.User); | |
h.Password(rmqSettings.Password); | |
}); | |
cfg.ReceiveEndpoint(host, rmqSettings.Endpoint, | |
ep => | |
{ | |
// Endpoint configuration stuff | |
}); | |
}); | |
Serilog.Log.Information("Starting..."); | |
_busControl.Start(); | |
Serilog.Log.Information("Started"); | |
WaitHandle.WaitOne(); | |
} | |
private static void Exit() | |
{ | |
Serilog.Log.Information("Exiting..."); | |
_busControl.Stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment