-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Closed
Labels
Description
Type of issue
Code doesn't work
Description
Problem 1
using App.WorkerService;
var builder = Host.CreateDefaultBuilder(args); // no CreateApplicationBuilder extension at .net 8.0
builder.Services.AddHostedService<Worker>(); // no AddHostedService extension at .net 8.0
IHost host = builder.Build();
host.Run();
The code above should be changed:
using App.WorkerService;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
var host = builder.Build();
host.Run();
Problem 2
using App.QueueService;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<MonitorLoop>();
builder.Services.AddHostedService<QueuedHostedService>();
builder.Services.AddSingleton<IBackgroundTaskQueue>(_ =>
{
if (!int.TryParse(builder.Configuration["QueueCapacity"], out var queueCapacity))
{
queueCapacity = 100;
}
return new DefaultBackgroundTaskQueue(queueCapacity);
});
IHost host = builder.Build();
MonitorLoop monitorLoop = host.Services.GetRequiredService<MonitorLoop>()!;
monitorLoop.StartMonitorLoop();
host.Run();
The code above also should be changed:
using App.QueueService;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<MonitorLoop>();
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue>(_ =>
{
if (!int.TryParse(hostContext.Configuration["QueueCapacity"], out var queueCapacity))
{
queueCapacity = 100;
}
return new DefaultBackgroundTaskQueue(queueCapacity);
});
});
IHost host = builder.Build();
MonitorLoop monitorLoop = host.Services.GetRequiredService<MonitorLoop>()!;
monitorLoop.StartMonitorLoop();
host.Run();
I've only installed Microsoft.Extensions.Hosting package.
Please let me know if I need install other package to get extensions: CreateApplicationBuilder, AddHostedService
Page URL
https://learn.microsoft.com/ko-kr/dotnet/core/extensions/workers#template-defaults
Content source URL
https://github.com/dotnet/docs/blob/main/docs/core/extensions/workers.md
Document Version Independent Id
ff379d26-0d85-6d74-67da-4a3cd260b04d
Article author
Metadata
- ID: ae847f16-ff74-ab68-7163-f713b447430c
- Service: dotnet-fundamentals