C
C#2y ago
Tantrim

❔ Unable to resolve service type for IHttpClientFactory

I'm hitting a wall on what to do. I have two .NET 7.0 applications. One of which an API and the other a Web App. Both apps are using the same version of System.Net.Http and builder.Services.AddHttpClient() doesn't seem to do anything. This is my first project working with API's like this so I'm a little lost on what to do. I'm willing to screen share if someone wants to hop in voice.
// API Project
// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHttpClient();

builder.Services.AddLogging();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();
app.Run();
// API Project
// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHttpClient();

builder.Services.AddLogging();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();
app.Run();
4 Replies
Tantrim
TantrimOP2y ago
Web App Proj
// Web App Project
// Index.cshtml.cs
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IHttpClientFactory _httpClientFactory;

public IndexModel(ILogger<IndexModel> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}


public async Task OnGet()
{
await GetAllContacts();
}

private async Task GetAllContacts()
{
var _client = _httpClientFactory.CreateClient();
var response = await _client.GetAsync("https://localhost:44320/api/Contacts");

List<ContactModel> contacts;

if (response.IsSuccessStatusCode)
{
var options = new JsonSerializerOptions
{
// allows javascript and c# variables to match. Variables in javascript are camelCase and C# is PascalCase
PropertyNameCaseInsensitive = true
};
string responseText = await response.Content.ReadAsStringAsync();

contacts = JsonSerializer.Deserialize<List<ContactModel>>(responseText, options);
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
// Web App Project
// Index.cshtml.cs
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IHttpClientFactory _httpClientFactory;

public IndexModel(ILogger<IndexModel> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}


public async Task OnGet()
{
await GetAllContacts();
}

private async Task GetAllContacts()
{
var _client = _httpClientFactory.CreateClient();
var response = await _client.GetAsync("https://localhost:44320/api/Contacts");

List<ContactModel> contacts;

if (response.IsSuccessStatusCode)
{
var options = new JsonSerializerOptions
{
// allows javascript and c# variables to match. Variables in javascript are camelCase and C# is PascalCase
PropertyNameCaseInsensitive = true
};
string responseText = await response.Content.ReadAsStringAsync();

contacts = JsonSerializer.Deserialize<List<ContactModel>>(responseText, options);
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
Tantrim
TantrimOP2y ago
Tantrim
TantrimOP2y ago
if you need anymore information please let me know
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server