Reputation: 1214
In a normal .net core service, the template would create a function like this for me :
public void Configure(IApplicationBuilder app)
{
...
}
In here, I can inject my services, and use them on startup, for instance, a database pre deploy script.
In a worker service template, this function is not generated for me, only the CreateHostbuilder function.
How do I go about either creating a the Configure() function, or to have a class fire/construct on startup in a worker service?
Upvotes: 0
Views: 917
Reputation: 11100
So you want to configure services, then use them from a console app.
From your question & comments it appears that you understand how to configure services;
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
...
.ConfigureServices(ConfigureServices);
public static void ConfigureServices(HostBuilderContext context, IServiceCollection serviceCollection) {
...
}
Now you have a couple of ways to use those services. You could start the host, then get services and use them in your main method. Exiting when your work is done and all services have been disposed.
public static async Task<int> Main(string[] args)
{
using (var host = CreateHostBuilder(args).Build())
{
await host.StartAsync();
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
...
lifetime.StopApplication();
await host.WaitForShutdownAsync();
}
return 0;
}
Or you could perform ongoing tasks by implementing IHostedService
/ BackgroundService
. When the IHost
is started, each registered IHostedService
will be started in the order they were registered, then stopped / cancelled when the host is shutdown.
public static async Task<int> Main(string[] args)
{
using (var host = CreateHostBuilder(args).Build())
{
await host.RunAsync();
}
return 0;
}
public static void ConfigureServices(HostBuilderContext context, IServiceCollection serviceCollection) {
serviceCollection.AddHostedService<Service>();
}
public class Service : BackgroundService {
public Service (...) {...}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Yield();
while (!stoppingToken.IsCancellationRequested)
{
...
}
}
}
Or some combination of the above.
Note that when you use the web host, it's an IHostedService
(GenericWebHostedService
) which actually starts the web server. Calling all the registered IStartupFilters
, including one that calls your Startup.Configure
method, to compile the request pipeline.
Upvotes: 1
Reputation: 1497
If you want to register an Interface or a service in order to inject them in other services, still you can register your services in ConfigureServices
.
in the following example, there are two services which register as Singleton
and Transient
.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddSingleton<IEmployeeService, EmployeeService>();
services.AddTransient<UserService>();
});
Upvotes: 0