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

60 lines
2.4 KiB
C#
Raw Normal View History

2025-12-04 22:20:00 +01:00
using Microsoft.AspNetCore.Components;
2025-11-06 10:20:00 +01:00
using Microsoft.Extensions.DependencyInjection;
2025-12-04 22:20:00 +01:00
using Microsoft.Extensions.Logging;
2025-11-06 10:20:00 +01:00
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
2025-12-01 17:56:17 +01:00
// Note: Authentication is handled by AuthService setting DefaultRequestHeaders.Authorization
2025-11-19 17:40:27 +01:00
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-12-03 13:33:38 +01:00
services.AddScoped<JobService>();
2025-11-19 16:57:42 +01:00
2025-12-02 13:23:03 +01:00
// Filter state services (scoped to maintain state during user session)
services.AddScoped<LayerFilterStateService>();
services.AddScoped<DataInboxFilterStateService>();
2025-12-04 22:20:00 +01:00
// SignalR Hub Service (singleton for global connection shared across all users)
services.AddSingleton<EntityChangeHubService>(sp =>
{
// For singleton, we can't inject scoped services directly
// We'll get them from the service provider when needed
var logger = sp.GetRequiredService<ILogger<EntityChangeHubService>>();
return new EntityChangeHubService(apiBaseUrl, sp, logger);
});
2025-11-06 10:20:00 +01:00
return services;
}
}