using Microsoft.AspNetCore.Components; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using DiunaBI.UI.Shared.Services; using DiunaBI.UI.Shared.Handlers; namespace DiunaBI.UI.Shared.Extensions; public static class ServiceCollectionExtensions { public static IServiceCollection AddSharedServices(this IServiceCollection services, string apiBaseUrl) { // HttpClient for API calls with logging // Ensure BaseAddress ends with / for proper relative URL resolution var baseUri = apiBaseUrl.EndsWith('/') ? apiBaseUrl : apiBaseUrl + "/"; Console.WriteLine($"🔧 Configuring HttpClient with BaseAddress: {baseUri}"); services.AddTransient(); // Configure named HttpClient with logging handler // Note: Authentication is handled by AuthService setting DefaultRequestHeaders.Authorization services.AddHttpClient("DiunaBI", client => { client.BaseAddress = new Uri(baseUri); Console.WriteLine($"✅ HttpClient BaseAddress set to: {client.BaseAddress}"); }) .AddHttpMessageHandler(); // Register a scoped HttpClient factory that services will use services.AddScoped(sp => { var factory = sp.GetRequiredService(); var client = factory.CreateClient("DiunaBI"); Console.WriteLine($"🏭 HttpClient created from factory. BaseAddress: {client.BaseAddress}"); return client; }); // Services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Filter state services (scoped to maintain state during user session) services.AddScoped(); services.AddScoped(); // SignalR Hub Service (singleton for global connection shared across all users) services.AddSingleton(sp => { // For singleton, we can't inject scoped services directly // We'll get them from the service provider when needed var logger = sp.GetRequiredService>(); return new EntityChangeHubService(apiBaseUrl, sp, logger); }); return services; } }