C#
C#

help

Root Question Message

Hercules
Hercules11/18/2022
❔ 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'

How i register my 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();
}


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; 
    }
Atakan / Cracker
Atakan / Cracker11/18/2022
You are not injecting HttpClient through interface
Atakan / Cracker
Atakan / Cracker11/18/2022
Can you try with IHttpClient ?
Hercules
Hercules11/18/2022
This made my app spin again
private readonly HttpClient _httpClient;
    public TextService(IHttpClientFactory httpClient)
    {
        _httpClient = httpClient.CreateClient();
    }
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy