0

I have the following code in asp.net core 3.1 project :

IArticleService.cs

namespace API.Interfaces
{
    public interface IArticleService
    {
        Task<ArticleDTO> GetArticleAsync(int articleId, int countryId, string userCookieId);
    }
}

ArticleService.cs

namespace API.Services
{
    public class ArticleService : IArticleService
    {
        private readonly IOptions<UrlsConfig> _urlConfig;
        private readonly HttpClient _httpClient;
        private readonly ILogger<ArticleService> _logger;

        public ArticleService(HttpClient httpClient, IOptions<UrlsConfig> urlConfig, ILogger<ArticleService> logger)
        {
            _urlConfig = urlConfig;
            _httpClient = httpClient;
            _logger = logger;
        }
        public async Task<ArticleDTO> GetArticleAsync(int articleId, int countryId, string userCookieId)
        {
            throw new System.NotImplementedException();
        }
    }
}

ProjectServiceCollectionExtensions.cs

public static IServiceCollection AddHttpClientDependencies(this IServiceCollection services)
{
    return services.AddHttpClient<IArticleService, ArticleService>();
}

Startup.cs

public virtual void ConfigureServices(IServiceCollection services) => services.AddHttpClientDependencies()

It is throwing an error :

Error CS0266 Cannot implicitly convert type 'Microsoft.Extensions.DependencyInjection.IHttpClientBuilder' to 'Microsoft.Extensions.DependencyInjection.IServiceCollection'. An explicit conversion exists (are you missing a cast?)

Can anyone help me here with some guidance to fix this issue?

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • Why does your method `AddHttpClientDependencies` returns an object of type `IServiceCollection`? May be you need to write in it: `return services.AddHttpClient().Services;`. – Iliar Turdushev Jun 24 '20 at 17:53
  • Thanks @IliarTurdushev for your response. I added the code and tried to compile the code. There are compile errors this time but now while running the code I am getting an error : Unable to resolve service for type 'System.Net.Http.HttpClient' while attempting to activate 'API.Services.ArticleService'. Any help on this is much appreciated – santosh kumar patro Jun 24 '20 at 20:44
  • 1
    Check this link https://github.com/dotnet/extensions/issues/1079. Maybe you faced the same problem. If your problem is different then, please, share code from your Startup.cs (that pieces of code that are important for the problem of resolving `HttpClient`). – Iliar Turdushev Jun 25 '20 at 03:13
  • Thanks @IliarTurdushev for your help. It helped me to fix the issue :) – santosh kumar patro Jun 25 '20 at 07:03

0 Answers0