Код: Выделить всё
public class QuickConsoleEntryPoint // the one I want to conditionally choose constructor
{
public QuickConsoleEntryPoint()
{
;
}
public QuickConsoleEntryPoint(QuickConsoleRunArgsManager argsParser)
{
;
}
}
public class QuickConsoleRunArgsManager
{
private readonly string[] _args;
private readonly string _hotelsArgsSwitch;
private readonly string _bookingsArgsSwitch;
public QuickConsoleRunArgsManager(QuickConsoleRunArgs runCommandArgs)
{
_args = runCommandArgs.args;
_hotelsArgsSwitch = "hotels";
_bookingsArgsSwitch = "bookings";
}
public (string HotelsFilename, string BookingsFilname) Parse()
{
var hotelRepositoryFilename = GetParameterValue(_hotelsArgsSwitch);
var bookingRepositoryFilename = GetParameterValue(_bookingsArgsSwitch);
if (hotelRepositoryFilename is null || bookingRepositoryFilename is null)
throw new ArgumentException("One or more underlying datafile names or parameters' switch not given properly.");
return (hotelRepositoryFilename, bookingRepositoryFilename);
}
private string? GetParameterValue(string key)
{
int index = Array.IndexOf(_args, "--" + key);
if (index >= 0 && _args.Length > index)
return _args[index + 1];
return null;
}
}
public class QuickConsoleRunArgs
{
public string[] args = [];
}
public class QuickConsoleEntryPoint
{
public QuickConsoleEntryPoint()
{
;
}
public QuickConsoleEntryPoint(QuickConsoleRunArgsManager argsParser)
{
;
}
}
Позвоните:
Код: Выделить всё
public static IServiceCollection AddQuickCommandLineArguments(this IServiceCollection services, string[] consoleRunArgs)
{
services.AddSingleton(sp => {
var options = new QuickConsoleRunArgs { args = consoleRunArgs };
return options;
});
services.AddSingleton();
return services;
}
Код: Выделить всё
public static IServiceCollection AddQuickConsole(this IServiceCollection services)
{
services.AddSingleton(
sp =>
{
if (sp.GetService() != null)
{
var argsManager = sp.GetService();
return new QuickConsoleEntryPoint(argsManager);
}
return new QuickConsoleEntryPoint();
});
return services;
}
Невозможно разрешить службу для типа «QuickConsole.QuickConsoleEntryPoint» при попытке активировать «ConsoleBookingApp.UserInterface.ConsoleAppInterface».
где ConsoleAppInterface — это просто класс, который будет использовать QuickConsoleEntryPoint, внедренный конструктором.
В чем может быть проблема и как ее исправить?< /п>
Подробнее здесь: https://stackoverflow.com/questions/793 ... dependency