I have a worker service created from the default .NET Core Template. I have two additional services I'd like to run, one emits an event and another consumes it. So ideally they both should be injected into the single Worker
hosted service. How can I make this happen? I tried the code as below, but I'm getting: System.InvalidOperationException: 'Unable to resolve service for type 'Receiver.IMessageReceiver' while attempting to activate 'MessageQueueReceiver.Worker'.'
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddSingleton<IMessageReceiver, MessageReceiver>(); services.AddSingleton<IWebhookMessageForwarder, WebhookMessageForwarder>(); services.AddHostedService<Worker>(); }); }
public class Worker : BackgroundService { private readonly ILogger _logger; private readonly IMessageReceiver _messageReceiver; private readonly IWebhookMessageForwarder _forwarder; public Worker(ILogger<Worker> logger, IMessageReceiver messageReceiver, IWebhookMessageForwarder forwarder) { _logger = logger; _messageReceiver = messageReceiver; _forwarder = forwarder; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _messageReceiver.MessageReceived += _forwarder.OnMessageReceived; _messageReceiver.RegisterHandlerAndReceiveMessages(); while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(TimeSpan.FromSeconds(60), stoppingToken); } } }