0

i'm trying to follow this example here

the only thing is i need to inject multiple hosted services instead of one like this

services.AddSimpleInjector(container, options =>
{
    // Registers the hosted service as singleton in Simple Injector
    // and hooks it onto the .NET Core Generic Host pipeline.
    options.AddHostedService<TimedHostedService<IProcessor>>();
    options.AddHostedService<MyHostedService<IProcessor>>();
});

the implementation of AddHostedService register one instance as singleton of IHostedService Interface I tried registering multiple implementations of IHostedService with services.TryAddEnumerable with no success any suggestions? Thanks

3
  • Can you elaborate on why the code above doesn't work for you?
    – Steven
    Mar 4, 2022 at 17:43
  • the ExecuteAsync method doesn't execute for the second registration
    – Redouane
    Mar 4, 2022 at 17:47
  • I'll try to add a code source to GitHub for a complete example
    – Redouane
    Mar 4, 2022 at 17:52

1 Answer 1

1

I'm unable to reproduce your issue. I used the following code in a Visual Studio 2022 ASP.NET Core MVC 6 project:

using System.Diagnostics;
using SimpleInjector;

var container = new Container();

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
var services = builder.Services;

services.AddControllersWithViews();

services.AddSimpleInjector(container, options =>
{
    options.AddHostedService<MyHostedService1>();
    options.AddHostedService<MyHostedService2>();
});

WebApplication app = builder.Build();

app.Services.UseSimpleInjector(container);

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

MyHostedService1 and MyHostedService2 are defined as follows:

class MyHostedService1 : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine($"Starting {this.GetType().Name}");
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

class MyHostedService2 : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine($"Starting {this.GetType().Name}");
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}

The Console output from the web application is the following:

Starting MyHostedService1
Starting MyHostedService2
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5071
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: f:\VS2022\HodgePodge\AspNetCore6MVC\

From this I conclude that both hosted services are started, which means the problem you have is not reproducible with the code you posted.

1
  • my mistake thanks steven I was blocking the context a silly mistake
    – Redouane
    Mar 5, 2022 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.