我的 Blazor (.Net 8) 应用程序中出现以下问题。我尝试使用 YARP Map Forwarder (Microsoft.Extensions.ServiceDiscovery.Yarp 8.0.2) 转发从 Bla 到达 Blazor 服务器的请求...
我的 Blazor (.Net 8) 应用程序中出现以下问题。我尝试使用 YARP Map Forwarder (Microsoft.Extensions.ServiceDiscovery.Yarp 8.0.2) 将从 Blazor 客户端到达 Blazor 服务器的请求转发到后端服务,作为一种反向代理。它总是返回 502 Bad Gateway。我在这里遗漏了什么?
...
app.UseRouting();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.UseAuthorization();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(BlazorProject.Client._Imports).Assembly);
// Always returns 502. As far as i can tell it forwards the request to itself instead of localhost:5000, but i may be wrong here
app.MapForwarder("/weather-forecast", "http://localhost:5000", transformBuilder =>
{
transformBuilder.AddRequestTransform(async transformContext =>
{
var accessToken = await transformContext.HttpContext.GetTokenAsync("access_token");
transformContext.ProxyRequest.Headers.Authorization = new("Bearer", accessToken);
});
}).RequireAuthorization();
我曾尝试添加 new ForwarderRequestConfig { Version = Version.Parse("1.1") }
到 MapForwarder,但没有成功。
我也尝试过 ProxyReqeust
通过操作 transformContext
来直接设置路线或者进行标头操作,但没有任何效果。
但是通过自定义处理也 app.Map
可以正常工作。
// Custom example
var httpClient = new HttpMessageInvoker(new SocketsHttpHandler
{
UseProxy = false,
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.None,
UseCookies = false,
EnableMultipleHttp2Connections = true,
ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current),
ConnectTimeout = TimeSpan.FromSeconds(15),
});
// Setup our own request transform class
var transformer = new CustomHttpTransformer();
var requestConfig = new ForwarderRequestConfig { ActivityTimeout = TimeSpan.FromSeconds(10)};
app.Map("/weather-forecast", async (HttpContext httpContext, IHttpForwarder forwarder) =>
{
var error = await forwarder.SendAsync(httpContext, "http://localhost:5000",
httpClient, requestConfig, transformer);
// Check if the operation was successful
if (error != ForwarderError.None)
{
var errorFeature = httpContext.GetForwarderErrorFeature();
throw errorFeature?.Exception ?? new Exception($"ForwarderError: {error}. Getting standard exception failed");
}
}).RequireAuthorization();
// Content of CustomHttpTransformer
public override async ValueTask TransformRequestAsync(HttpContext httpContext,
HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken)
{
// Copy all request headers
await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken);
// Customize the query string:
var queryContext = new QueryTransformContext(httpContext.Request);
// Set custom uri
proxyRequest.RequestUri = RequestUtilities.MakeDestinationAddress($"{destinationPrefix}", httpContext.Request.Path, queryContext.QueryString);
// Suppress the original request header, use the one from the destination Uri.
proxyRequest.Headers.Host = null;
// Set authorization Headers
var accessToken = await httpContext.GetTokenAsync("access_token");
proxyRequest.Headers.Authorization = new("Bearer", accessToken);
}