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:
@@ -5,7 +5,7 @@ using EnvelopeGenerator.Server.Client.Options;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
|
||||
public class DocumentService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
public class DocumentService(IHttpClientFactory httpClientFactory, IOptions<ApiOptions> apiOptions)
|
||||
{
|
||||
private readonly ApiOptions _api = apiOptions.Value;
|
||||
|
||||
@@ -16,7 +16,8 @@ public class DocumentService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
/// <exception cref="HttpRequestException">Thrown when the API request fails.</exception>
|
||||
public async Task<byte[]?> GetDocumentAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
var response = await http.GetAsync($"{_api.BaseUrl}/api/Document/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var response = await http.GetAsync($"/api/Document/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user