无法通过Ocelot网关连接SignalR服务的问题求助
问题:Ocelot网关代理SignalR服务连接失败,直接访问正常
我可通过JavaScript直接访问并使用SignalR服务,但通过Ocelot网关尝试连接时出现错误。已检查CORS设置、ocelot.json配置文件及DownstreamScheme参数,调整过CORS配置,也查阅了Stack Overflow相关案例,但问题仍未解决。
Ocelot Program.cs代码
using Ocelot.DependencyInjection; using Ocelot.Middleware; using Ocelot.Values; var builder = WebApplication.CreateBuilder(args); // Configure CORS builder.Services .AddCors(options => { options.AddPolicy("CorsPolicy", policy => { policy .WithOrigins(builder.Configuration.GetSection("CORS:Origins").Get<string[]>() ?? []) .AllowAnyMethod() .AllowCredentials() .AllowAnyHeader() .SetIsOriginAllowedToAllowWildcardSubdomains() .SetIsOriginAllowed(x => true).AllowAnyMethod().AllowAnyHeader(); }); }); // add ocelot builder.Services.AddOcelot(builder.Configuration); builder.Services.AddSignalR(); builder.Configuration.AddJsonFile("ocelot.json"); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseCors(policy => policy.AllowAnyHeader() .AllowAnyMethod() .SetIsOriginAllowed(origin => true) .AllowCredentials()); app.UseRouting(); app.UseHttpsRedirection(); app.UseAuthorization(); app.UseEndpoints(_ => { }); app.UseCors("CorsPolicy"); app.UseWebSockets(); await app.UseOcelot(); await app.RunAsync();
ocelot.json配置内容
{ "Routes": [ { "DownstreamPathTemplate": "/api/Authentication/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Authentication/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Teacher/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Teacher/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Certification/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Certification/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Student/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Student/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/SchoolLevel/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/SchoolLevel/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Country/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Country/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Expertise/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Expertise/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Parameter/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 6001 } ], "UpstreamPathTemplate": "/api/Parameter/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/LessonComment/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 7001 } ], "UpstreamPathTemplate": "/api/LessonComment/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/LessonComplaint/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 7001 } ], "UpstreamPathTemplate": "/api/LessonComplaint/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/LessonScheduled/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 7001 } ], "UpstreamPathTemplate": "/api/LessonScheduled/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/LessonCancellation/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 7001 } ], "UpstreamPathTemplate": "/api/LessonCancellation/{url}", "Priority": 1, "UpstreamHttpMethod": [ "Get", "Post", "Put" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/api/Chat/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 8001 } ], "UpstreamPathTemplate": "/api/Chat/{url}", "Priority": 1, "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ], "LoadBalancerOptions": { "Type": "LeastConnection" } }, { "DownstreamPathTemplate": "/Chat/{url}", "DownstreamScheme": "ws", "DownstreamHostAndPorts": [ { "Host": "192.168.1.23", "Port": 8001 } ], "UpstreamPathTemplate": "/Chat/{url}", "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ] } ], "GlobalConfiguration": { "BaseUrl": "https://192.168.1.23:9001" } }
错误与请求情况
- 错误提示:WebSocket连接建立失败
- 网络请求异常:SignalR握手请求阶段出现错误
内容的提问来源于stack exchange,提问作者Earendur
相关产品推荐
相关产品推荐

