具有简单注入器的多个托管服务

发布于 2025-01-11 19:00:16 字数 655 浏览 0 评论 0原文

我正在尝试按照这个示例这里

唯一的是我需要注入多个托管服务,而不是像这样的一个

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>>();
});

AddHostedService 的实现将一个实例注册为 IHostedService 接口的单例 我尝试使用 services.TryAddEnumerable 注册 IHostedService 的多个实现,但没有成功,有什么建议吗? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2025-01-18 19:00:16

我无法重现您的问题。我在 Visual Studio 2022 ASP.NET Core MVC 6 项目中使用了以下代码:

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();

MyHostedService1MyHostedService2 定义如下:

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;
}

Web 应用程序的控制台输出如下:

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\

由此我得出结论,两个托管服务都已启动,这意味着您所遇到的问题无法用您发布的代码重现。

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(
quot;Starting {this.GetType().Name}");
        return Task.CompletedTask;
    }

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

class MyHostedService2 : IHostedService
{
    public Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine(
quot;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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文