Files
DiunaBI/DiunaBI.UI.Shared/Extensions/ServiceCollectionExtensions.cs

43 lines
1.6 KiB
C#
Raw Normal View History

2025-11-06 10:20:00 +01:00
using Microsoft.Extensions.DependencyInjection;
using DiunaBI.UI.Shared.Services;
2025-11-19 17:30:36 +01:00
using DiunaBI.UI.Shared.Handlers;
2025-11-06 10:20:00 +01:00
namespace DiunaBI.UI.Shared.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSharedServices(this IServiceCollection services, string apiBaseUrl)
{
2025-11-19 17:30:36 +01:00
// HttpClient for API calls with logging
2025-11-19 16:57:42 +01:00
// Ensure BaseAddress ends with / for proper relative URL resolution
var baseUri = apiBaseUrl.EndsWith('/') ? apiBaseUrl : apiBaseUrl + "/";
2025-11-19 17:30:36 +01:00
2025-11-19 17:40:27 +01:00
Console.WriteLine($"🔧 Configuring HttpClient with BaseAddress: {baseUri}");
2025-11-19 17:30:36 +01:00
services.AddTransient<HttpLoggingHandler>();
2025-11-19 17:40:27 +01:00
// Configure named HttpClient with logging handler
services.AddHttpClient("DiunaBI", client =>
2025-11-19 17:30:36 +01:00
{
client.BaseAddress = new Uri(baseUri);
2025-11-19 17:40:27 +01:00
Console.WriteLine($"✅ HttpClient BaseAddress set to: {client.BaseAddress}");
2025-11-19 17:30:36 +01:00
})
.AddHttpMessageHandler<HttpLoggingHandler>();
2025-11-19 17:40:27 +01:00
// Register a scoped HttpClient factory that services will use
services.AddScoped<HttpClient>(sp =>
2025-11-19 17:30:36 +01:00
{
2025-11-19 17:40:27 +01:00
var factory = sp.GetRequiredService<IHttpClientFactory>();
var client = factory.CreateClient("DiunaBI");
Console.WriteLine($"🏭 HttpClient created from factory. BaseAddress: {client.BaseAddress}");
return client;
});
2025-11-19 16:57:42 +01:00
2025-11-06 10:20:00 +01:00
// Services
services.AddScoped<AuthService>();
services.AddScoped<LayerService>();
2025-12-01 12:55:47 +01:00
services.AddScoped<DataInboxService>();
2025-11-19 16:57:42 +01:00
2025-11-06 10:20:00 +01:00
return services;
}
}