Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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