Skip to content

If I want to write a CLI application... #4625

Answered by jack775544
atrauzzi asked this question in Q&A
Discussion options

You must be logged in to vote

From what I have seen, Aspire components are built using the IHostApplicationBuilder which is not limited to web apps, but can also be used for console apps. You can read more about how to use it for a console app here.

If you want a short lived appplication, you can do something like this

var builder = Host.CreateApplicationBuilder();

// This is stuff from Aspire
builder.AddServiceDefaults();
builder.AddNpgsqlDbContext<MyDbContext>("Postgres");

using var host = builder.Build();

var dbContext = host.Services.GetRequiredService<MyDbContext>();
// Do stuff here

Or if you want a long lived application, you can implement a hosted service and run the host instead of just building it.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@atrauzzi
Comment options

@jack775544
Comment options

Answer selected by davidfowl
@atrauzzi
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants