C
C#4mo ago
SWEETPONY

System.AggregateException: 'Some services are not able to be constructed

I stuck with this problem and don't understand how to fix it: InvalidOperationException: Unable to resolve service for type 'BackForFront.Settings.FlightLegAggregatorSettings' while attempting to activate 'BackForFront.Services.FlightLegAggregator.FlightLegAggregatorApi'. api:
public FlightLegAggregatorApi(
HttpClientFactory httpClientFactory,
CommonSettings commonSettings,
FlightLegAggregatorSettings flightLegAggregatorOptions) :
base(httpClientFactory, commonSettings.ContextHeaderName!)
{
this.createFlightLegEndpoint = flightLegAggregatorOptions.FlightLegCreateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
this.updateFlightLegEndpoint = flightLegAggregatorOptions.FlightLegUpdateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
}
public FlightLegAggregatorApi(
HttpClientFactory httpClientFactory,
CommonSettings commonSettings,
FlightLegAggregatorSettings flightLegAggregatorOptions) :
base(httpClientFactory, commonSettings.ContextHeaderName!)
{
this.createFlightLegEndpoint = flightLegAggregatorOptions.FlightLegCreateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
this.updateFlightLegEndpoint = flightLegAggregatorOptions.FlightLegUpdateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
}
how I register it:
public static IServiceCollection RegisterFlightLegCreationServices(this IServiceCollection services, IConfiguration configuration)
{
BindSettings<FlightLegAggregatorSettings>(services, configuration, "FlightLegAggregator");
BindSettings<SolverManagerSettings>(services, configuration, "SolverManager");
BindSettings<DmnSettings>(services, configuration, "Dmn");
BindSettings<ReadModelClientSettings>(services, configuration, "ReadModelClient");

services.AddTransient<HttpClientFactory>();
services.AddTransient<FlightLegAggregatorApiBase, FlightLegAggregatorApi>();
services.AddScoped<FlightLegAggregatorServiceBase, FlightLegAggregatorService>();

return services;
}
public static IServiceCollection RegisterFlightLegCreationServices(this IServiceCollection services, IConfiguration configuration)
{
BindSettings<FlightLegAggregatorSettings>(services, configuration, "FlightLegAggregator");
BindSettings<SolverManagerSettings>(services, configuration, "SolverManager");
BindSettings<DmnSettings>(services, configuration, "Dmn");
BindSettings<ReadModelClientSettings>(services, configuration, "ReadModelClient");

services.AddTransient<HttpClientFactory>();
services.AddTransient<FlightLegAggregatorApiBase, FlightLegAggregatorApi>();
services.AddScoped<FlightLegAggregatorServiceBase, FlightLegAggregatorService>();

return services;
}
3 Replies
Alen Alex
Alen Alex4mo ago
If you want to use the configuration which is binded to a class (Options Pattern), you need to wrap the FlightLegAggregatorSettings in IOptions<FlightLegAggregatorSettings>, instead of simply passing the class. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-8.0#the-options-pattern
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
Alen Alex
Alen Alex4mo ago
So, instead of what you have replace your current api like this
c#
public FlightLegAggregatorApi(
HttpClientFactory httpClientFactory,
CommonSettings commonSettings,
IOptions<FlightLegAggregatorSettings> flightLegAggregatorOptions //Changing from FlightLegAggregatorSettings to //IOptions<FlightLegAggregatorSettings>
) :
base(httpClientFactory, commonSettings.ContextHeaderName!)
{
this.createFlightLegEndpoint = flightLegAggregatorOptions.FlightLegCreateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
this.updateFlightLegEndpoint = flightLegAggregatorOptions.FlightLegUpdateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
}
c#
public FlightLegAggregatorApi(
HttpClientFactory httpClientFactory,
CommonSettings commonSettings,
IOptions<FlightLegAggregatorSettings> flightLegAggregatorOptions //Changing from FlightLegAggregatorSettings to //IOptions<FlightLegAggregatorSettings>
) :
base(httpClientFactory, commonSettings.ContextHeaderName!)
{
this.createFlightLegEndpoint = flightLegAggregatorOptions.FlightLegCreateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
this.updateFlightLegEndpoint = flightLegAggregatorOptions.FlightLegUpdateUri ?? throw new ArgumentNullException(nameof(flightLegAggregatorOptions));
}
SWEETPONY
SWEETPONY4mo ago
ah, thanks!