我们使用信号 r 来驱动我们的前端进行更新,它一直在 100% 工作,直到我们升级到 .net core 2.2请参阅下面的启动,这些方法确实被调用并且正常的后端调用可以正常工作,但是只要 Signal r 尝试从前端连接,我就会收到一个 cors 错误。我试图让所有通过cors,但这也没有用。protected IServiceProvider ConfigureServicesImplementation(IServiceCollection services) { services.Configure<DatabaseConfiguration>(Configuration.GetSection(DatabaseConfigurationName)); services.Configure<TokenProviderConfiguration>(Configuration.GetSection(TokenProviderConfigurationName)); services.AddOptions(); services.AddCors(); services.AddAutoMapper(); services.AddAutofac(); services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped<DatabaseMigrator>(); services.AddScoped<AdminTenantDbInitialiser>(); services.AddScoped<TenantDbInitialiser>(); services.AddScoped<HubBase>(); services.AddScoped<HubService>(); services.AddSignalR() .AddJsonProtocol(options => { options.PayloadSerializerSettings.Converters.Add(new StringEnumConverter { }); });至于从前端连接,请参见下面的代码。public startHubConnection(): void { if (!this.hubConnection) { const currentUser = this.authenticationService.getCurrentUser(); this.hubConnection = new HubConnectionBuilder().withUrl(this.url + '/globalhub?Authorization=Bearer ' + currentUser.accessToken).build(); } this.hubConnection .start() .then(() => { this.isHubConnected = true; if (this.firstRun) { this.firstRun = false; this.manageHubConnections(); }
2 回答
湖上湖
TA贡献2003条经验 获得超2个赞
CORS 中间件必须位于应用程序中您想要支持跨域请求的任何已定义端点之前,因此您应该UseCors在之前调用UseSignalR:
...
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseSignalR(routes =>
{
routes.MapHub<HubBase>("/globalhub");
});
...
- 2 回答
- 0 关注
- 71 浏览
添加回答
举报
0/150
提交
取消