CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

[Question] Dependency Injection error when using Host.CreateDefaultBuilder with RunCommandLineApplicationAsync

Open thesobercoder opened this issue 4 years ago • 1 comments

I am trying to use the RunCommandLineApplicationAsync with Host.CreateDefaultBuilder, but the program errors out saying that ILogger is not configured, which is weird because ILogger is already comes configured with CreateDefaultBuilder. Am I doing something wrong here?

Here is my main -

internal static class Program
{
	internal static async Task Main(string[] args)
	{
		var host = CreateHostBuilder(args);

		await host.RunCommandLineApplicationAsync<App>(args, (appcon) =>
		{
			appcon.Conventions
			.UseDefaultConventions()
			.UseConstructorInjection();
		});
	}

	internal static IHostBuilder CreateHostBuilder(string[] args)
	{
		return Host.CreateDefaultBuilder(args)
		.ConfigureServices((hostContext, services) =>
		{
		   
		});
	}
}

and here is the App.cs -

internal class App
{
	private readonly ILogger _logger;

	public App(ILogger logger)
	{
		_logger = logger;
	}

	private void OnExecute()
	{
		_logger.LogInformation("Executed");
	}
}

thesobercoder avatar Jun 25 '21 12:06 thesobercoder

I think this matches your problem: https://stackoverflow.com/a/57590076

natemcmaster avatar Oct 22 '21 02:10 natemcmaster

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Oct 23 '22 02:10 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Nov 07 '22 02:11 github-actions[bot]