I have written a REST API using C# that runs on my server. When it is started, I want to preload some data from the database based on the organization that has purchased it (name, address, etc). However, I can't seem to figure out the name of the initial program that is called which is normally something like application.cs or program.cs. Can someone provide the name of the initial program or tell me where I can find it?

Dani AI

Generated

Short answer for : the startup file depends on the framework and project template. For ASP.NET Core the canonical entry is usually Program.cs (older templates also include Startup.cs for configuration). For .NET 6+ minimal-hosting templates the top-level Program.cs holds the startup. For classic ASP.NET Web API the bootstrap point is Application_Start in Global.asax (or an OWIN Startup class if Katana/OWIN is used). As hinted, knowing which of these applies will determine where to place preload logic.

A safe, framework-agnostic pattern for preloading (ASP.NET Core shown) is: build the host/app, create a service scope, resolve a DbContext or other scoped services, run an idempotent seeder, then start the host. Example (Program.Main style):

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        var db = services.GetRequiredService<MyDbContext>();
        DbSeeder.Seed(db); // or await DbSeeder.SeedAsync(db) if async
    }

    host.Run();
}

Minimal-hosting (.NET 6+) equivalent:

var builder = WebApplication.CreateBuilder(args);
// register services...
var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
    await DbSeeder.SeedAsync(db);
}

app.Run();

Notes and cautions: always create a scope to resolve scoped services (do not use the root provider), make seeders idempotent, prefer async seeding and avoid blocking on async (no .Result/.Wait), and use an IHostedService/BackgroundService for long-running or nonblocking initialization. For legacy projects, put equivalent seeding in Application_Start or the OWIN Startup method after DI is configured.

Are you using ASP.NET Core?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.