-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
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 consolediAdditional 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:
- https://stackoverflow.com/q/68392429/19879469
- https://stackoverflow.com/q/66996319/19879469
- https://stackoverflow.com/q/41407221/19879469
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.