Refactor HTTP client management and service lifetimes

Updated DependencyInjection.cs to change ISmsSender and
IEnvelopeSmsHandler lifetimes from Singleton to Scoped,
ensuring per-request instantiation. Added Microsoft.Extensions.Http
package to EnvelopeGenerator.Server.Client.csproj for enhanced
HttpClient handling. Refactored AnnotationService, AuthService,
DocumentService, EnvelopeReceiverService, SignatureCacheService,
and SignatureService to use IHttpClientFactory, improving
flexibility and testability. Introduced a named HttpClient
"EnvelopeGenerator.Server" in Program.cs for internal API calls,
and removed the previous HttpClient setup using HttpContextAccessor.
Added necessary using directives for System.Net.Http across
service files to support these changes.
This commit is contained in:
2026-06-22 17:35:00 +02:00
parent 106e62a912
commit b6ec5307b6
9 changed files with 39 additions and 33 deletions

View File

@@ -53,6 +53,9 @@ try
builder.Services.AddControllers();
builder.Services.AddHttpClient();
// Named HttpClient for internal API calls (same domain, uses relative paths)
builder.Services.AddHttpClient("EnvelopeGenerator.Server");
// CORS Policy
var allowedOrigins = config.GetSection("AllowedOrigins").Get<string[]>() ??
throw new InvalidOperationException("AllowedOrigins section is missing in the configuration.");
@@ -289,20 +292,6 @@ try
// HttpClient for server-side components (e.g., MainLayout with FontLoader)
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<HttpClient>(sp =>
{
var httpContextAccessor = sp.GetRequiredService<IHttpContextAccessor>();
var request = httpContextAccessor.HttpContext?.Request;
var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient();
if (request != null)
{
httpClient.BaseAddress = new Uri($"{request.Scheme}://{request.Host}");
}
return httpClient;
});
// Business Services (Server specific)
builder.Services.AddScoped<DocumentService>();