首页 > 解决方案 > Yarp.ReverseProxy 后面的 SignalR 导致超时导致服务器不响应

问题描述

我已经使用以下代码实现了一个 yarp.reverse 代理服务器:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpProxy();
        services.AddCors(options =>
        {
            options.AddPolicy("customPolicy", builder =>
            {
                builder.AllowAnyOrigin();                    
            });                
        });
    }
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHttpProxy httpProxy)
    {
        // Configure our own HttpMessageInvoker for outbound calls for proxy operations
        var httpClient = new HttpMessageInvoker(new SocketsHttpHandler()
        {
            UseProxy = false,
            AllowAutoRedirect = false,
            AutomaticDecompression = DecompressionMethods.None,
            UseCookies = false
        });

        // Setup our own request transform class
        var transformer = new CustomTransformer(); // or HttpTransformer.Default;
        var requestOptions = new RequestProxyOptions { Timeout = TimeSpan.FromSeconds(100) };

        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.Map("/{**catch-all}", async httpContext =>
            {
                httpContext.Request.Headers["Connection"] = "upgrade";
                
                await httpProxy.ProxyAsync(httpContext, "http://192.168.178.80:5000", httpClient, requestOptions, transformer);
                
                var errorFeature = httpContext.Features.Get<IProxyErrorFeature>();
                save_log(httpContext.Request.Path, "/", "http://192.168.178.80:5000" + httpContext.Request.Path, "3");
                // Check if the proxy operation was successful
                if (errorFeature != null)
                {
                    var error = errorFeature.Error;
                    var exception = errorFeature.Exception;
                }
            });
        });
    }

在另一个应用程序中,一个 SignalR 服务器遵循此示例:https ://docs.microsoft.com/en-GB/aspnet/core/tutorials/signalr?view=aspnetcore-5.0&tabs=visual-studio

代理服务器工作并将请求转发到 signalR 服务器。但是 signalR 客户端无法连接到 signalR 服务器。我总是因为错误而断开连接

错误:服务器超时未收到来自服务器的消息。

在 Java 脚本控制台中。但 SSE 已连接,您可以在以下浏览器状态报告中看到:

signalr.js:2156 [2021-03-25T13:19:29.970Z] 信息:SSE 连接到 https://localhost:44318/chatHub?id=IqKD6P0NsUY9Is6OSrMusQ

问题似乎出在代理服务器上,因为如果我直接调用该站点,它就可以工作。有人知道我的代理有什么问题以及如何解决吗?

标签: c#asp.net-coreproxysignalr

解决方案


推荐阅读