Refactor HttpClient usage and simplify configuration
Updated HttpClient setup to use named clients for API calls and DevExpress components, improving resource management and scalability. Scoped a default HttpClient for PdfViewer requirements. Removed ApiOptions configuration binding for simplification. Updated FontLoader to use IHttpClientFactory to align with modern best practices.
This commit is contained in:
@@ -8,14 +8,19 @@ using DevExpress.XtraReports.Services;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
|
||||
// HTTP Client (uses Server's YARP proxy)
|
||||
builder.Services.AddScoped(sp => new HttpClient {
|
||||
// Named HttpClient for API calls (both for services and DevExpress components)
|
||||
builder.Services.AddHttpClient("EnvelopeGenerator.Server", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
|
||||
});
|
||||
|
||||
// Default HttpClient (DevExpress PdfViewer requires this)
|
||||
builder.Services.AddScoped(sp => new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
|
||||
});
|
||||
|
||||
// Configuration Options
|
||||
builder.Services.Configure<ApiOptions>(opts =>
|
||||
builder.Configuration.GetSection(ApiOptions.SectionName).Bind(opts));
|
||||
builder.Services.Configure<PdfViewerOptions>(opts =>
|
||||
builder.Configuration.GetSection(PdfViewerOptions.SectionName).Bind(opts));
|
||||
|
||||
@@ -51,5 +56,5 @@ builder.Services.AddScoped<IReportProviderAsync, CustomReportProvider>();
|
||||
ReportStorageWebExtension.RegisterExtensionGlobal(new InMemoryReportStorageWebExtension());
|
||||
|
||||
var host = builder.Build();
|
||||
await FontLoader.LoadFonts(host.Services.GetRequiredService<HttpClient>(), new List<string> { "opensans.ttf" });
|
||||
await FontLoader.LoadFonts(host.Services.GetRequiredService<IHttpClientFactory>(), new List<string> { "opensans.ttf" });
|
||||
await host.RunAsync();
|
||||
|
||||
Reference in New Issue
Block a user