使用简单注入器的多托管服务
Multiple Hosted Service With Simple Injector
我正在尝试效仿这个例子here
唯一的问题是我需要注入多个托管服务而不是像这样的一个
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 的多个实现,但没有成功,有什么建议吗?
谢谢
我无法重现您的问题。我在 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();
MyHostedService1
和MyHostedService2
定义如下:
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\
据此我得出结论,两个托管服务都已启动,这意味着您遇到的问题无法用您发布的代码重现。
我正在尝试效仿这个例子here
唯一的问题是我需要注入多个托管服务而不是像这样的一个
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 的多个实现,但没有成功,有什么建议吗? 谢谢
我无法重现您的问题。我在 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();
MyHostedService1
和MyHostedService2
定义如下:
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\
据此我得出结论,两个托管服务都已启动,这意味着您遇到的问题无法用您发布的代码重现。