Skip to content

Instantly share code, notes, and snippets.

@hd9
Created February 7, 2020 20:27
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/ebe143cb01114eae341fdcc71e3ba8de to your computer and use it in GitHub Desktop.
Save hd9/ebe143cb01114eae341fdcc71e3ba8de to your computer and use it in GitHub Desktop.
MassTransit - Console Hosted Service initialization
// **********************************************
// Fore more information, visit:
// https://blog.hildenco.com/2018/08/masstransit-real-alternative-to.html
// **********************************************
using System.Threading;
using System.Threading.Tasks;
using MassTransit;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SampleService
{
public class MassTransitConsoleHostedService :
IHostedService
{
readonly IBusControl _bus;
readonly ILogger _logger;
public MassTransitConsoleHostedService(IBusControl bus, ILoggerFactory loggerFactory)
{
_bus = bus;
_logger = loggerFactory.CreateLogger<MassTransitConsoleHostedService>();
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting bus");
await _bus.StartAsync(cancellationToken).ConfigureAwait(false);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping bus");
return _bus.StopAsync(cancellationToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment