natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

thesobercoder opened this issue · 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");
	}
}

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

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.

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.