SignalR Core 适用于 localhost 但不适用于 Azure 应用服务 [英] SignalR Core works on localhost but not on Azure App Service

查看:51
本文介绍了SignalR Core 适用于 localhost 但不适用于 Azure 应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端程序(.Net 4.8 上的 WPF 应用程序)和一个 Web API(.Net Core 3.1).我试图让两者通过 SignalR Core 进行通信.当两者都在我的 PC 上本地运行时(即在 localhost 上),它可以完美运行.但是,一旦我将 API 发布到 Azure 应用服务(并将 WPF 应用指向新 URL),它就无法工作.SignalR 建立了连接,但是当 API 向 WPF 应用发送数据时,应用永远不会收到它.

I have a client program (WPF app on .Net 4.8) and a Web API (.Net Core 3.1). I'm trying to get the two to communicate over SignalR Core. It works perfectly when both are running locally on my PC (i.e. on localhost). But as soon as I publish my API to Azure App Service (and point the WPF app to the new URL) it doesnt work. SignalR establishes a connection, but when the API sends data to the WPF app, the app never receives it.

我不确定它是否与 CORS 相关.Azure 应用服务上的 CORS 已禁用.在我的 Web API 上,我是这个 Startup.cs:

I'm not sure if it's related to CORS. CORS on Azure App Service is disabled. On my Web API, I this this 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 地址.

I think I once read that you cannot have AllowAnyOrigin() with SignalR. You need to specify the desired origins. But I'm not sure what my origin will be as this is a WPF app running on various users computers, all with different domains/IP addresses.

就像我说的,当一切都在 loclahost 上时,它可以完美运行.但是一旦 API 在 Azure 应用服务上,两者就设法建立 SignalR 连接,仅此而已.WPF 应用未从 API 接收任何数据.

Like I said, it works perfectly when everything is on loclahost. But as soon as the API is on Azure App Service, the two manage to establish a SignalR connection, but that's about it. No data is received by the WPF app from the API.

有什么想法吗?

推荐答案

是的,您需要指定来源.CORS 方法的顺序也很重要.并且您需要 CORS 来在您的 Framework 应用程序和 .NET Core 应用程序之间进行通信.所以你可以试试这个:

Yes you need to specify the origins. And order of the CORS methods also maters. And you need the CORS to comunicate beetween your Framework app and the .NET Core app. So you can try this:

services.AddCors(options =>
{
    options.AddPolicy(CorsPolicy, builder => builder.WithOrigins("https://yourFrameworkApp.azurewebsites.com")
        .AllowAnyHeader()
        .AllowAnyMethod()
        .AllowCredentials()
        .SetIsOriginAllowed((host) => true));
});

您还可以在客户端查看连接到集线器时遇到的错误.例如,当客户端调用端点到您的集线器进行协商时,您可以:

Also you can see on the client side what error do you have when connecting to the hub. For example, when the client side call the endpoint to your hub to negotiate you can have:

CORS 错误 - 尝试像下面那样添加它.

CORS error - try add it like bellow.

404 not found - 您指向了错误的控制器/集线器.

404 not found - you are pointing to the wrong controller/hub.

405 方法不允许 - 这是当用户无权调用您的中心中的方法时.

405 method not allowed - this is when the user is not authorized to call methods in your hub.

此外,在您的启动(配置方法)中检查您是否添加了 Web 套接字:

Also, on your Startup (Configure method) check if you are adding Web Sockets:

app.UseWebSockets();

编辑:您也可以尝试使用 Azure SignalR,这样您就无需担心此类问题.

Edit: you can also try using Azure SignalR so you don't need to worry about this type of problems.

这篇关于SignalR Core 适用于 localhost 但不适用于 Azure 应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆