Add SocketsHttpHandler
This commit is contained in:
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
||||
using ZXing.Net.Maui.Controls;
|
||||
using System.Net.Http;
|
||||
using Jugenddienst_Stunden.ViewModels;
|
||||
using System.Net;
|
||||
|
||||
namespace Jugenddienst_Stunden;
|
||||
|
||||
@@ -32,7 +33,7 @@ public static class MauiProgram {
|
||||
//#if DEBUG
|
||||
// if (string.IsNullOrWhiteSpace(GlobalVar.ApiKey)) {
|
||||
// GlobalVar.ApiKey = Preferences.Default.Get("apiKey",
|
||||
// "MTQxfHNkdFptQkNZTXlPT3ZyMHNBZDl0UnVxNExMRXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=");
|
||||
// "MTQxfHNkdFptQkNZTXlPT3ZyMHxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=");
|
||||
// GlobalVar.Name = Preferences.Default.Get("name", "Testserver: Isabell");
|
||||
// GlobalVar.Surname = Preferences.Default.Get("surname", "Biasi");
|
||||
// GlobalVar.EmployeeId = Preferences.Default.Get("EmployeeId", 141);
|
||||
@@ -42,6 +43,10 @@ public static class MauiProgram {
|
||||
// builder.Logging.AddDebug();
|
||||
//#endif
|
||||
|
||||
// ApiClient registrieren: SocketsHttpHandler als Primary Handler (vermeidet AndroidMessageHandler-Castfehler)
|
||||
var apiOptions = new Infrastructure.ApiOptions { BaseUrl = GlobalVar.ApiUrl, Timeout = TimeSpan.FromSeconds(15) };
|
||||
builder.Services.AddApiHttpClient(apiOptions);
|
||||
|
||||
// DI: AlertService für globale Alerts (z. B. leere ApiUrl)
|
||||
builder.Services.AddSingleton<IAlertService, AlertService>();
|
||||
|
||||
@@ -50,17 +55,23 @@ public static class MauiProgram {
|
||||
|
||||
// DI: ApiOptions IMMER aus aktuellen Settings erzeugen (nicht beim Start einfrieren)
|
||||
builder.Services.AddTransient(sp => new ApiOptions {
|
||||
BaseUrl = sp.GetRequiredService<IAppSettings>().ApiUrl, Timeout = TimeSpan.FromSeconds(15)
|
||||
BaseUrl = sp.GetRequiredService<IAppSettings>().ApiUrl,
|
||||
Timeout = TimeSpan.FromSeconds(15)
|
||||
});
|
||||
|
||||
// Token Provider soll ebenfalls aus Settings/Preferences lesen
|
||||
builder.Services.AddSingleton<ITokenProvider, SettingsTokenProvider>();
|
||||
|
||||
// HttpClient + ApiClient
|
||||
// Configure HttpClient with RequestLoggingHandler and disable automatic redirects for diagnosis
|
||||
// Configure HttpClient with SocketsHttpHandler (managed) and RequestLoggingHandler
|
||||
builder.Services.AddTransient<RequestLoggingHandler>();
|
||||
builder.Services.AddSingleton<HttpClient>(sp => {
|
||||
var nativeHandler = new HttpClientHandler { AllowAutoRedirect = false };
|
||||
var nativeHandler = new SocketsHttpHandler {
|
||||
AllowAutoRedirect = false,
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
|
||||
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
|
||||
ConnectTimeout = TimeSpan.FromSeconds(10)
|
||||
};
|
||||
var logging = sp.GetRequiredService<RequestLoggingHandler>();
|
||||
logging.InnerHandler = nativeHandler;
|
||||
// HttpClient.Timeout will be adjusted by ApiClient if needed
|
||||
@@ -95,16 +106,16 @@ public static class MauiProgram {
|
||||
// DI: Validatoren
|
||||
builder.Services.AddSingleton<IHoursValidator, HoursValidator>();
|
||||
|
||||
// DI: Services & Repositories
|
||||
builder.Services.AddSingleton<IHoursRepository, HoursRepository>();
|
||||
builder.Services.AddSingleton<IHoursService, HoursService>();
|
||||
builder.Services.AddSingleton<IAuthService, AuthService>();
|
||||
// DI: Services & Repositories
|
||||
builder.Services.AddSingleton<IHoursRepository, HoursRepository>();
|
||||
builder.Services.AddSingleton<IHoursService, HoursService>();
|
||||
builder.Services.AddSingleton<IAuthService, AuthService>();
|
||||
|
||||
// DI: Views/ViewModels
|
||||
builder.Services.AddTransient<ViewModels.StundenViewModel>();
|
||||
builder.Services.AddTransient<Views.StundenPage>();
|
||||
builder.Services.AddTransient<ViewModels.LoginViewModel>();
|
||||
builder.Services.AddTransient<Views.LoginPage>();
|
||||
// DI: Views/ViewModels
|
||||
builder.Services.AddTransient<ViewModels.StundenViewModel>();
|
||||
builder.Services.AddTransient<Views.StundenPage>();
|
||||
builder.Services.AddTransient<ViewModels.LoginViewModel>();
|
||||
builder.Services.AddTransient<Views.LoginPage>();
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user