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:
@@ -1,3 +1,4 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
using EnvelopeGenerator.Server.Client.Options;
|
||||
@@ -8,7 +9,7 @@ namespace EnvelopeGenerator.Server.Client.Services;
|
||||
/// <summary>
|
||||
/// Client service for managing cached signatures via API.
|
||||
/// </summary>
|
||||
public class SignatureCacheService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
public class SignatureCacheService(IHttpClientFactory httpClientFactory, IOptions<ApiOptions> apiOptions)
|
||||
{
|
||||
private readonly ApiOptions _api = apiOptions.Value;
|
||||
|
||||
@@ -17,8 +18,9 @@ public class SignatureCacheService(HttpClient http, IOptions<ApiOptions> apiOpti
|
||||
SignatureCaptureDto signature,
|
||||
CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var response = await http.PostAsJsonAsync(
|
||||
$"{_api.BaseUrl}/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
signature,
|
||||
cancel);
|
||||
|
||||
@@ -33,8 +35,9 @@ public class SignatureCacheService(HttpClient http, IOptions<ApiOptions> apiOpti
|
||||
string envelopeKey,
|
||||
CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var response = await http.GetAsync(
|
||||
$"{_api.BaseUrl}/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
cancel);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
@@ -53,8 +56,9 @@ public class SignatureCacheService(HttpClient http, IOptions<ApiOptions> apiOpti
|
||||
string envelopeKey,
|
||||
CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var response = await http.DeleteAsync(
|
||||
$"{_api.BaseUrl}/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
||||
cancel);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
|
||||
Reference in New Issue
Block a user