Skip to content

Add dotnet new consoledi project template #42517

@mariusz96

Description

@mariusz96

Is your feature request related to a problem? Please describe.

Add dotnet new consoledi project template for console app with depenency injection and IHostBuilder defaults.

Describe the solution you'd like

+ dotnet new consoledi

Additional context

I frequently find myself writing console apps like this:

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<ConsoleApplication>();
builder.Services.AddSingleton<IGreeter, Greeter>();
// Other registrations...

using IHost host = builder.Build();
await host.Services.GetRequiredService<ConsoleApplication>().RunAsync();
public class ConsoleApplication(IGreeter greeter)
{
    public Task RunAsync()
    {
        greeter.Greet(Environment.UserName);
        return Task.CompletedTask;
    }
}
public interface IGreeter
{
    void Greet(string userName);
}
public class Greeter(ILogger<Greeter> logger) : IGreeter
{
    public void Greet(string userName) =>
        logger.LogInformation("Hello {UserName}!", userName);
}

Very similar to basic: https://github.com/dotnet/docs/blob/a265ac4ba40b8cb6a3ecd8abc1b4e35d153e6617/docs/core/extensions/snippets/configuration/console-di-disposable/Program.cs but complete with appsettings.{environment}.json and put in non-static class just like dotnet new worker.

But always figuring out the template for the current version of .NET is not fun and anyone wanting to use it needs to rediscover it on their own:

So the template would make it easier and might even lead some people to start using it in their console apps. Additionally, it would be great for quick demos, experiments, learning, and such.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions