Created
February 7, 2020 20:27
-
-
Save hd9/ebe143cb01114eae341fdcc71e3ba8de to your computer and use it in GitHub Desktop.
MassTransit - Console Hosted Service initialization
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: | |
// 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