我有一个客户端程序 (.Net 4.8上的WPF应用程序)和一个Web API(.Net Core 3.1)。我正在尝试让它们通过SignalR Core进行通信。当两者都在我的PC本地运行时(即在localhost上),它可以完美地工作。但是,一旦我将API发布到Azure App Service(并将WPF应用程序指向新的URL),它就无法正常工作。SignalR建立了连接,但是当API向WPF应用程序发送数据时,应用程序却从未接收到。我不确定是否与CORS有关。Azure App Service上的CORS已禁用。在我的Web API上,我有以下Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
});
string connectionString = Configuration.GetConnectionString("eBallDatabase");
services.AddDbContext<EBallContext>(options =>
options.UseSqlServer(connectionString));
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
});
services.AddApplicationInsightsTelemetry();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("corsPolicy");
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/chatHub");
});
}
我记得曾经读过一篇文章,说SignalR不能使用AllowAnyOrigin(),需要指定所需的来源。但是我不确定我的来源是什么,因为这是一个在各个用户计算机上运行的WPF应用程序,所有计算机都有不同的域名/IP地址。
就像我说的,在所有东西都在本地主机上时,它可以完美地工作。但是一旦API在Azure App Service上,它们就能够建立SignalR连接,但这就是全部。WPF应用程序没有从API接收到任何数据。
有什么想法吗?