Refactor HttpClient and HttpContextAccessor setup
Moved HttpContextAccessor registration into the configuration
of the named HttpClient ("EnvelopeGenerator.Server") to support
server-side rendering (SSR) scenarios. Updated the HttpClient
to dynamically set its BaseAddress based on the current request's
scheme and host using HttpContextAccessor. Removed standalone
HttpContextAccessor registration and updated related comments.
This commit is contained in:
@@ -53,8 +53,21 @@ try
|
|||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
|
|
||||||
// Named HttpClient for internal API calls (same domain, uses relative paths)
|
// HttpContextAccessor needed for SSR HttpClient configuration
|
||||||
builder.Services.AddHttpClient("EnvelopeGenerator.Server");
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
|
// Named HttpClient for internal API calls
|
||||||
|
builder.Services.AddHttpClient("EnvelopeGenerator.Server", (sp, client) =>
|
||||||
|
{
|
||||||
|
var httpContextAccessor = sp.GetRequiredService<IHttpContextAccessor>();
|
||||||
|
var request = httpContextAccessor.HttpContext?.Request;
|
||||||
|
|
||||||
|
if (request != null)
|
||||||
|
{
|
||||||
|
// Set base address to current host for SSR scenarios
|
||||||
|
client.BaseAddress = new Uri($"{request.Scheme}://{request.Host}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// CORS Policy
|
// CORS Policy
|
||||||
var allowedOrigins = config.GetSection("AllowedOrigins").Get<string[]>() ??
|
var allowedOrigins = config.GetSection("AllowedOrigins").Get<string[]>() ??
|
||||||
@@ -290,9 +303,6 @@ try
|
|||||||
.AddEnvelopeGeneratorServices(config);
|
.AddEnvelopeGeneratorServices(config);
|
||||||
#pragma warning restore CS0618
|
#pragma warning restore CS0618
|
||||||
|
|
||||||
// HttpClient for server-side components (e.g., MainLayout with FontLoader)
|
|
||||||
builder.Services.AddHttpContextAccessor();
|
|
||||||
|
|
||||||
// Business Services (Server specific)
|
// Business Services (Server specific)
|
||||||
builder.Services.AddScoped<DocumentService>();
|
builder.Services.AddScoped<DocumentService>();
|
||||||
builder.Services.AddScoped<AuthService>();
|
builder.Services.AddScoped<AuthService>();
|
||||||
|
|||||||
Reference in New Issue
Block a user