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 System.Text.Json;
|
||||
using EnvelopeGenerator.Server.Client.Models;
|
||||
@@ -6,13 +7,14 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
|
||||
public class SignatureService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
public class SignatureService(IHttpClientFactory httpClientFactory, IOptions<ApiOptions> apiOptions)
|
||||
{
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
|
||||
|
||||
public async Task<IReadOnlyList<SignatureDto>> GetAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
var url = $"{apiOptions.Value.BaseUrl}/api/Signature/{Uri.EscapeDataString(envelopeKey)}";
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var url = $"/api/Signature/{Uri.EscapeDataString(envelopeKey)}";
|
||||
var response = await http.GetAsync(url, cancel);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
|
||||
Reference in New Issue
Block a user