C
C#3y ago
Hercules

❔ Why I am having issues to register my services.

The error I get is the following
System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: StatsByAlfaLaval.Application.Services.TextService.ITextService Lifetime: Scoped ImplementationType: StatsByAlfaLaval.Application.Services.TextService.TextService': Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'StatsByAlfaLaval.Application.Services.TextService.TextService'
System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: StatsByAlfaLaval.Application.Services.TextService.ITextService Lifetime: Scoped ImplementationType: StatsByAlfaLaval.Application.Services.TextService.TextService': Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'StatsByAlfaLaval.Application.Services.TextService.TextService'
How i register my services
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddScoped<IAuthenticationService, AuthenticationService>();
services.AddScoped<ITextService, TextService>();

return services;
}
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddScoped<IAuthenticationService, AuthenticationService>();
services.AddScoped<ITextService, TextService>();

return services;
}
Then i implement that into the program.cs where my builder resides
var builder = WebApplication.CreateBuilder(args);
{
builder.Services
.AddApplication()
.AddInfrastructure();

builder.Services.AddControllers();
}
var builder = WebApplication.CreateBuilder(args);
{
builder.Services
.AddApplication()
.AddInfrastructure();

builder.Services.AddControllers();
}
Does anyone have any idea why this is not working? If i remove my latest service it works fluently. I suspect my httpclient request
public class TextService : ITextService
{
private readonly HttpClient _httpClient;
public TextService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public class TextService : ITextService
{
private readonly HttpClient _httpClient;
public TextService(HttpClient httpClient)
{
_httpClient = httpClient;
}
3 Replies
Cracker
Cracker3y ago
You are not injecting HttpClient through interface Can you try with IHttpClient ?
Hercules
HerculesOP3y ago
This made my app spin again
private readonly HttpClient _httpClient;
public TextService(IHttpClientFactory httpClient)
{
_httpClient = httpClient.CreateClient();
}
private readonly HttpClient _httpClient;
public TextService(IHttpClientFactory httpClient)
{
_httpClient = httpClient.CreateClient();
}
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?