This is my Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddTransient<DatabaseSeeder>();
builder.Services.AddScoped<IWeatherForecastService, WeatherForecastService>();
builder.Services.AddDbContext<RestaurantDbContext>();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var databaseSeeder = serviceProvider.GetRequiredService<DatabaseSeeder>();
try
{
await databaseSeeder.SeedDatabaseAsync();
Console.WriteLine("Seed is up.");
}
//...
}
and this is DatabaseSeeder.cs
public class DatabaseSeeder
{
private readonly RestaurantDbContext _context;
public DatabaseSeeder(RestaurantDbContext context);
public async Task SeedDatabaseAsync()
{
_context.Restaurants.AddRange(GetRestaurants());
await _context.SaveChangesAsync();
}
public IEnumerable<Restaurant> GetRestaurants()
{
IEnumerable<Restaurant> restaurants = new List<Restaurant>()
{
// here some samples
};
return restaurants;
}
}
So how can I change my code in Program.cs
to use IHostedService
to seed data ?
I don't understand what is IHostedService
, can't find good source to read about it either.
I dont understand what is IHostedService, cant find good source to read about it either.
I would argue that the official Background tasks with hosted services in ASP.NET Core documentation is quite good source of the knowledge on the topic. It also contains a section which covers the IHostedService
interface. Basically this interface defines methods for objects that are managed by the host and usually is used to perform some background activity alongside your ASP.NET Core app or in the so called worker service
How to add data seed method with IHostedService
I would argue that this is not a correct abstraction to use for such a task. Usually you don't want your service to be up and running and serving requests before the data seed is finished since it can result in some inconsistent behaviour for obvious reasons. And if you decide to use IHostedService
infrastructure to perform such task you will need to perform some custom synchronization (and possibly some readiness probes). See for a bit more complicated Worker Service caching example in the docs which got the synchronization wrong originally (see this answer).
So I would argue that leaving the data seeding in the Program.cs is just fine.